xref: /openbmc/linux/tools/testing/selftests/watchdog/watchdog-test.c (revision 4da722ca19f30f7db250db808d1ab1703607a932)
1 /*
2  * Watchdog Driver Test Program
3  */
4 
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <sys/ioctl.h>
13 #include <linux/types.h>
14 #include <linux/watchdog.h>
15 
16 int fd;
17 const char v = 'V';
18 
19 /*
20  * This function simply sends an IOCTL to the driver, which in turn ticks
21  * the PC Watchdog card to reset its internal timer so it doesn't trigger
22  * a computer reset.
23  */
24 static void keep_alive(void)
25 {
26     int dummy;
27     int ret;
28 
29     ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
30     if (!ret)
31         printf(".");
32 }
33 
34 /*
35  * The main program.  Run the program with "-d" to disable the card,
36  * or "-e" to enable the card.
37  */
38 
39 static void term(int sig)
40 {
41     int ret = write(fd, &v, 1);
42 
43     close(fd);
44     if (ret < 0)
45 	printf("\nStopping watchdog ticks failed (%d)...\n", errno);
46     else
47 	printf("\nStopping watchdog ticks...\n");
48     exit(0);
49 }
50 
51 int main(int argc, char *argv[])
52 {
53     int flags;
54     unsigned int ping_rate = 1;
55     int ret;
56     int i;
57 
58     setbuf(stdout, NULL);
59 
60     fd = open("/dev/watchdog", O_WRONLY);
61 
62     if (fd == -1) {
63 	printf("Watchdog device not enabled.\n");
64 	exit(-1);
65     }
66 
67     for (i = 1; i < argc; i++) {
68         if (!strncasecmp(argv[i], "-d", 2)) {
69             flags = WDIOS_DISABLECARD;
70             ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
71             if (!ret)
72                 printf("Watchdog card disabled.\n");
73         } else if (!strncasecmp(argv[i], "-e", 2)) {
74             flags = WDIOS_ENABLECARD;
75             ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
76             if (!ret)
77                 printf("Watchdog card enabled.\n");
78         } else if (!strncasecmp(argv[i], "-t", 2) && argv[2]) {
79             flags = atoi(argv[i + 1]);
80             ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
81             if (!ret)
82                 printf("Watchdog timeout set to %u seconds.\n", flags);
83             i++;
84         } else if (!strncasecmp(argv[i], "-p", 2) && argv[2]) {
85             ping_rate = strtoul(argv[i + 1], NULL, 0);
86             printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
87             i++;
88         } else {
89             printf("-d to disable, -e to enable, -t <n> to set "
90                 "the timeout,\n-p <n> to set the ping rate, and ");
91             printf("run by itself to tick the card.\n");
92             printf("Parameters are parsed left-to-right in real-time.\n");
93             printf("Example: %s -d -t 10 -p 5 -e\n", argv[0]);
94             goto end;
95         }
96     }
97 
98     printf("Watchdog Ticking Away!\n");
99 
100     signal(SIGINT, term);
101 
102     while(1) {
103 	keep_alive();
104 	sleep(ping_rate);
105     }
106 end:
107     ret = write(fd, &v, 1);
108     if (ret < 0)
109 	printf("Stopping watchdog ticks failed (%d)...\n", errno);
110     close(fd);
111     return 0;
112 }
113