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 
28     printf(".");
29     ioctl(fd, WDIOC_KEEPALIVE, &dummy);
30 }
31 
32 /*
33  * The main program.  Run the program with "-d" to disable the card,
34  * or "-e" to enable the card.
35  */
36 
37 static void term(int sig)
38 {
39     int ret = write(fd, &v, 1);
40 
41     close(fd);
42     if (ret < 0)
43 	printf("\nStopping watchdog ticks failed (%d)...\n", errno);
44     else
45 	printf("\nStopping watchdog ticks...\n");
46     exit(0);
47 }
48 
49 int main(int argc, char *argv[])
50 {
51     int flags;
52     unsigned int ping_rate = 1;
53     int ret;
54 
55     setbuf(stdout, NULL);
56 
57     fd = open("/dev/watchdog", O_WRONLY);
58 
59     if (fd == -1) {
60 	printf("Watchdog device not enabled.\n");
61 	exit(-1);
62     }
63 
64     if (argc > 1) {
65 	if (!strncasecmp(argv[1], "-d", 2)) {
66 	    flags = WDIOS_DISABLECARD;
67 	    ioctl(fd, WDIOC_SETOPTIONS, &flags);
68 	    printf("Watchdog card disabled.\n");
69 	    goto end;
70 	} else if (!strncasecmp(argv[1], "-e", 2)) {
71 	    flags = WDIOS_ENABLECARD;
72 	    ioctl(fd, WDIOC_SETOPTIONS, &flags);
73 	    printf("Watchdog card enabled.\n");
74 	    goto end;
75 	} else if (!strncasecmp(argv[1], "-t", 2) && argv[2]) {
76 	    flags = atoi(argv[2]);
77 	    ioctl(fd, WDIOC_SETTIMEOUT, &flags);
78 	    printf("Watchdog timeout set to %u seconds.\n", flags);
79 	    goto end;
80 	} else if (!strncasecmp(argv[1], "-p", 2) && argv[2]) {
81 	    ping_rate = strtoul(argv[2], NULL, 0);
82 	    printf("Watchdog ping rate set to %u seconds.\n", ping_rate);
83 	} else {
84 	    printf("-d to disable, -e to enable, -t <n> to set " \
85 		"the timeout,\n-p <n> to set the ping rate, and \n");
86 	    printf("run by itself to tick the card.\n");
87 	    goto end;
88 	}
89     }
90 
91     printf("Watchdog Ticking Away!\n");
92 
93     signal(SIGINT, term);
94 
95     while(1) {
96 	keep_alive();
97 	sleep(ping_rate);
98     }
99 end:
100     ret = write(fd, &v, 1);
101     if (ret < 0)
102 	printf("Stopping watchdog ticks failed (%d)...\n", errno);
103     close(fd);
104     return 0;
105 }
106