de855a874967c3c6d160d3540d276fc276352ef5
[watchdog-test-framework.git] / watchdog-test.c
1 /* watchdog-test framework
2  * Copyright (C) 2014 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 <unistd.h>
22 #include <time.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <linux/watchdog.h>
26
27 #define WATCHDOG_DEVICE "/dev/watchdog"
28 #define WATCHDOG_TIMEOUT 30
29
30 int
31 main ()
32 {
33   int fd, i;
34   int timeout;
35   char input[256];
36   time_t start_t, t;
37
38   setvbuf (stdout, NULL, _IONBF, 0);
39
40   printf ("\n");
41   printf ("Welcome to the watchdog test framework.\n");
42   printf ("You should read the README file and run this in the guest.\n");
43   printf ("DO NOT RUN IT IN THE HOST!\n");
44   printf ("\n");
45   printf ("The test is as follows:\n");
46   printf ("(1) I will set up the watchdog with a %d second timeout.\n",
47           WATCHDOG_TIMEOUT);
48   printf ("(2) I will ping the watchdog for %d seconds.  During this time\n"
49           "    the guest should run normally.\n",
50           WATCHDOG_TIMEOUT * 2);
51   printf ("(3) I will stop pinging the watchdog and just count up.  If the\n"
52           "    virtual watchdog device is set correctly, then the watchdog\n"
53           "    action (eg. reboot) should happen around the %d second mark.\n",
54           WATCHDOG_TIMEOUT);
55   printf ("\n");
56   printf ("Do you want to start the test?  Type \"yes\" without quotes:\n");
57
58   if (fgets (input, sizeof input, stdin) == NULL ||
59       strcmp (input, "yes") != 0) {
60     printf ("Exiting the program.\n");
61     exit (EXIT_SUCCESS);
62   }
63
64   printf ("\n");
65   printf ("Setting up the watchdog (%s) with a %d second timeout.\n",
66           WATCHDOG_DEVICE, WATCHDOG_TIMEOUT);
67
68   sync ();
69   fd = open (WATCHDOG_DEVICE, O_WRONLY);
70   if (fd == -1) {
71     perror (WATCHDOG_DEVICE);
72     exit (EXIT_FAILURE);
73   }
74
75   timeout = WATCHDOG_TIMEOUT;
76   if (ioctl (fd, WDIOC_SETTIMEOUT, &timeout) == -1) {
77     perror ("ioctl: WDIOC_SETTIMEOUT: error setting timeout");
78     exit (EXIT_FAILURE);
79   }
80
81   if (ioctl (fd, WDIOC_GETTIMEOUT, &timeout) == -1)
82     perror ("ioctl: WDIOC_GETTIMEOUT");
83   else {
84     printf ("Timeout is set to %d seconds.\n");
85     if (timeout != WATCHDOG_TIMEOUT)
86       printf ("Note: some watchdog devices don't support setting exact timeout values.\n");
87   }
88
89   printf ("\n");
90   printf ("Pinging the watchdog for %d seconds ...\n", WATCHDOG_TIMEOUT*2);
91   printf ("\n");
92
93   time (&start_t);
94   for (;;) {
95     time (&t);
96     if (t - start_t > WATCHDOG_TIMEOUT * 2)
97       break;
98     printf ("%d... ", (int) (t - start_t));
99
100     sync ();
101
102     sleep (3);
103
104     if (ioctl (fd, WDIOC_KEEPALIVE, 0) == -1)
105       perror ("ioctl: WDIOC_KEEPALIVE");
106     printf ("* ");
107   }
108
109   printf ("\n");
110   printf ("Stopping pings.\n");
111   printf ("The watchdog action should happen at around %d mark.\n");
112   printf ("\n");
113
114   time (&start_t);
115   for (;;) {
116     time (&t);
117     printf ("%d... ", (int) (t - start_t));
118     sync ();
119     sleep (3);
120   }
121
122   /*NOTREACHED*/
123   exit (EXIT_SUCCESS);
124 }