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 <getopt.h>
13 #include <sys/ioctl.h>
14 #include <linux/types.h>
15 #include <linux/watchdog.h>
16 
17 #define DEFAULT_PING_RATE	1
18 
19 int fd;
20 const char v = 'V';
21 static const char sopts[] = "bdehp:t:";
22 static const struct option lopts[] = {
23 	{"bootstatus",          no_argument, NULL, 'b'},
24 	{"disable",             no_argument, NULL, 'd'},
25 	{"enable",              no_argument, NULL, 'e'},
26 	{"help",                no_argument, NULL, 'h'},
27 	{"pingrate",      required_argument, NULL, 'p'},
28 	{"timeout",       required_argument, NULL, 't'},
29 	{NULL,                  no_argument, NULL, 0x0}
30 };
31 
32 /*
33  * This function simply sends an IOCTL to the driver, which in turn ticks
34  * the PC Watchdog card to reset its internal timer so it doesn't trigger
35  * a computer reset.
36  */
37 static void keep_alive(void)
38 {
39 	int dummy;
40 	int ret;
41 
42 	ret = ioctl(fd, WDIOC_KEEPALIVE, &dummy);
43 	if (!ret)
44 		printf(".");
45 }
46 
47 /*
48  * The main program.  Run the program with "-d" to disable the card,
49  * or "-e" to enable the card.
50  */
51 
52 static void term(int sig)
53 {
54 	int ret = write(fd, &v, 1);
55 
56 	close(fd);
57 	if (ret < 0)
58 		printf("\nStopping watchdog ticks failed (%d)...\n", errno);
59 	else
60 		printf("\nStopping watchdog ticks...\n");
61 	exit(0);
62 }
63 
64 static void usage(char *progname)
65 {
66 	printf("Usage: %s [options]\n", progname);
67 	printf(" -b, --bootstatus    Get last boot status (Watchdog/POR)\n");
68 	printf(" -d, --disable       Turn off the watchdog timer\n");
69 	printf(" -e, --enable        Turn on the watchdog timer\n");
70 	printf(" -h, --help          Print the help message\n");
71 	printf(" -p, --pingrate=P    Set ping rate to P seconds (default %d)\n", DEFAULT_PING_RATE);
72 	printf(" -t, --timeout=T     Set timeout to T seconds\n");
73 	printf("\n");
74 	printf("Parameters are parsed left-to-right in real-time.\n");
75 	printf("Example: %s -d -t 10 -p 5 -e\n", progname);
76 }
77 
78 int main(int argc, char *argv[])
79 {
80 	int flags;
81 	unsigned int ping_rate = DEFAULT_PING_RATE;
82 	int ret;
83 	int c;
84 	int oneshot = 0;
85 
86 	setbuf(stdout, NULL);
87 
88 	fd = open("/dev/watchdog", O_WRONLY);
89 
90 	if (fd == -1) {
91 		printf("Watchdog device not enabled.\n");
92 		exit(-1);
93 	}
94 
95 	while ((c = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
96 		switch (c) {
97 		case 'b':
98 			flags = 0;
99 			oneshot = 1;
100 			ret = ioctl(fd, WDIOC_GETBOOTSTATUS, &flags);
101 			if (!ret)
102 				printf("Last boot is caused by: %s.\n", (flags != 0) ?
103 					"Watchdog" : "Power-On-Reset");
104 			else
105 				printf("WDIOC_GETBOOTSTATUS errno '%s'\n", strerror(errno));
106 			break;
107 		case 'd':
108 			flags = WDIOS_DISABLECARD;
109 			ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
110 			if (!ret)
111 				printf("Watchdog card disabled.\n");
112 			else
113 				printf("WDIOS_DISABLECARD errno '%s'\n", strerror(errno));
114 			break;
115 		case 'e':
116 			flags = WDIOS_ENABLECARD;
117 			ret = ioctl(fd, WDIOC_SETOPTIONS, &flags);
118 			if (!ret)
119 				printf("Watchdog card enabled.\n");
120 			else
121 				printf("WDIOS_ENABLECARD errno '%s'\n", strerror(errno));
122 			break;
123 		case 'p':
124 			ping_rate = strtoul(optarg, NULL, 0);
125 			if (!ping_rate)
126 				ping_rate = DEFAULT_PING_RATE;
127 			printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
128 			break;
129 		case 't':
130 			flags = strtoul(optarg, NULL, 0);
131 			ret = ioctl(fd, WDIOC_SETTIMEOUT, &flags);
132 			if (!ret)
133 				printf("Watchdog timeout set to %u seconds.\n", flags);
134 			else
135 				printf("WDIOC_SETTIMEOUT errno '%s'\n", strerror(errno));
136 			break;
137 		default:
138 			usage(argv[0]);
139 			goto end;
140 		}
141 	}
142 
143 	if (oneshot)
144 		goto end;
145 
146 	printf("Watchdog Ticking Away!\n");
147 
148 	signal(SIGINT, term);
149 
150 	while (1) {
151 		keep_alive();
152 		sleep(ping_rate);
153 	}
154 end:
155 	ret = write(fd, &v, 1);
156 	if (ret < 0)
157 		printf("Stopping watchdog ticks failed (%d)...\n", errno);
158 	close(fd);
159 	return 0;
160 }
161