1 /*
2 * Watchdog Driver Test Program
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <sys/ioctl.h>
11 #include <linux/types.h>
12 #include <linux/watchdog.h>
13
14 int fd;
15
16 /*
17 * This function simply sends an IOCTL to the driver, which in turn ticks
18 * the PC Watchdog card to reset its internal timer so it doesn't trigger
19 * a computer reset.
20 */
keep_alive(void)21 static void keep_alive(void)
22 {
23 int dummy;
24
25 ioctl(fd, WDIOC_KEEPALIVE, &dummy);
26 }
27
28 /*
29 * The main program. Run the program with "-d" to disable the card,
30 * or "-e" to enable the card.
31 */
main(int argc,char * argv[])32 int main(int argc, char *argv[])
33 {
34 int flags;
35
36 fd = open("/dev/watchdog", O_WRONLY);
37
38 if (fd == -1) {
39 fprintf(stderr, "Watchdog device not enabled.\n");
40 fflush(stderr);
41 exit(-1);
42 }
43 flags = 10;
44 ioctl(fd,WDIOC_SETTIMEOUT,&flags);
45
46 if (argc > 1) {
47 if (!strncasecmp(argv[1], "-d", 2)) {
48 flags = WDIOS_DISABLECARD;
49 ioctl(fd, WDIOC_SETOPTIONS, &flags);
50 fprintf(stderr, "Watchdog card disabled.\n");
51 fflush(stderr);
52 exit(0);
53 } else if (!strncasecmp(argv[1], "-e", 2)) {
54 flags = WDIOS_ENABLECARD;
55 ioctl(fd, WDIOC_SETOPTIONS, &flags);
56 fprintf(stderr, "Watchdog card enabled.\n");
57 fflush(stderr);
58 exit(0);
59 } else {
60 fprintf(stderr, "-d to disable, -e to enable.\n");
61 fprintf(stderr, "run by itself to tick the card.\n");
62 fflush(stderr);
63 exit(0);
64 }
65 } else {
66 fprintf(stderr, "Watchdog Ticking Away!\n");
67 fflush(stderr);
68 }
69
70 while(1) {
71 keep_alive();
72 sleep(1);
73 }
74 }
75