blob: 082db2334ab271ca6d1ee76a195ce63cc2fb8718 [file] [log] [blame]
Randy Dunlap56fb9e52006-05-21 20:58:10 -07001/*
2 * Watchdog Driver Test Program
3 */
4
Arnd Bergmann9dd8d5f2016-07-19 17:41:22 +02005#include <errno.h>
Randy Dunlap56fb9e52006-05-21 20:58:10 -07006#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <unistd.h>
10#include <fcntl.h>
Devendra Nagacad19fa2012-05-17 15:07:48 +053011#include <signal.h>
Randy Dunlap56fb9e52006-05-21 20:58:10 -070012#include <sys/ioctl.h>
13#include <linux/types.h>
14#include <linux/watchdog.h>
15
16int fd;
Timur Tabi5a2d3de2016-06-21 18:00:15 -050017const char v = 'V';
Randy Dunlap56fb9e52006-05-21 20:58:10 -070018
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 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -070024static void keep_alive(void)
Randy Dunlap56fb9e52006-05-21 20:58:10 -070025{
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020026 int dummy;
27 int ret;
Randy Dunlap56fb9e52006-05-21 20:58:10 -070028
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020029 ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
30 if (!ret)
31 printf(".");
Randy Dunlap56fb9e52006-05-21 20:58:10 -070032}
33
34/*
35 * The main program. Run the program with "-d" to disable the card,
36 * or "-e" to enable the card.
37 */
Devendra Nagacad19fa2012-05-17 15:07:48 +053038
Randy Dunlap4b1c2f42012-07-23 10:46:11 -070039static void term(int sig)
Devendra Nagacad19fa2012-05-17 15:07:48 +053040{
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020041 int ret = write(fd, &v, 1);
Arnd Bergmann9dd8d5f2016-07-19 17:41:22 +020042
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020043 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);
Devendra Nagacad19fa2012-05-17 15:07:48 +053049}
50
Randy Dunlap56fb9e52006-05-21 20:58:10 -070051int main(int argc, char *argv[])
52{
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020053 int flags;
54 unsigned int ping_rate = 1;
55 int ret;
56 int i;
James Hogandfc33382010-04-05 11:31:29 +010057
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020058 setbuf(stdout, NULL);
Timur Tabiee279c22016-06-21 18:00:14 -050059
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020060 fd = open("/dev/watchdog", O_WRONLY);
Randy Dunlap56fb9e52006-05-21 20:58:10 -070061
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020062 if (fd == -1) {
63 printf("Watchdog device not enabled.\n");
64 exit(-1);
65 }
Randy Dunlap56fb9e52006-05-21 20:58:10 -070066
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020067 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 }
Randy Dunlap56fb9e52006-05-21 20:58:10 -070097
Eugeniu Rosca0c528da2017-07-01 14:57:25 +020098 printf("Watchdog Ticking Away!\n");
Timur Tabif15d7112015-06-29 11:46:17 -050099
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200100 signal(SIGINT, term);
Devendra Nagacad19fa2012-05-17 15:07:48 +0530101
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200102 while (1) {
103 keep_alive();
104 sleep(ping_rate);
105 }
Devendra Naga3c2a6182012-05-14 23:42:02 +0530106end:
Eugeniu Rosca0c528da2017-07-01 14:57:25 +0200107 ret = write(fd, &v, 1);
108 if (ret < 0)
109 printf("Stopping watchdog ticks failed (%d)...\n", errno);
110 close(fd);
111 return 0;
Randy Dunlap56fb9e52006-05-21 20:58:10 -0700112}