blob: 3da822967ee0e13d1a2dbcd88d2d75f5a4aca370 [file] [log] [blame]
Randy Dunlap56fb9e52006-05-21 20:58:10 -07001/*
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>
Devendra Nagacad19fa2012-05-17 15:07:48 +053010#include <signal.h>
Randy Dunlap56fb9e52006-05-21 20:58:10 -070011#include <sys/ioctl.h>
12#include <linux/types.h>
13#include <linux/watchdog.h>
14
15int fd;
16
17/*
18 * This function simply sends an IOCTL to the driver, which in turn ticks
19 * the PC Watchdog card to reset its internal timer so it doesn't trigger
20 * a computer reset.
21 */
Ladinu Chandrasingheb7ed6982009-09-22 16:43:42 -070022static void keep_alive(void)
Randy Dunlap56fb9e52006-05-21 20:58:10 -070023{
24 int dummy;
25
26 ioctl(fd, WDIOC_KEEPALIVE, &dummy);
27}
28
29/*
30 * The main program. Run the program with "-d" to disable the card,
31 * or "-e" to enable the card.
32 */
Devendra Nagacad19fa2012-05-17 15:07:48 +053033
Randy Dunlap4b1c2f42012-07-23 10:46:11 -070034static void term(int sig)
Devendra Nagacad19fa2012-05-17 15:07:48 +053035{
36 close(fd);
37 fprintf(stderr, "Stopping watchdog ticks...\n");
38 exit(0);
39}
40
Randy Dunlap56fb9e52006-05-21 20:58:10 -070041int main(int argc, char *argv[])
42{
James Hogandfc33382010-04-05 11:31:29 +010043 int flags;
44
Randy Dunlap56fb9e52006-05-21 20:58:10 -070045 fd = open("/dev/watchdog", O_WRONLY);
46
47 if (fd == -1) {
48 fprintf(stderr, "Watchdog device not enabled.\n");
49 fflush(stderr);
50 exit(-1);
51 }
52
53 if (argc > 1) {
54 if (!strncasecmp(argv[1], "-d", 2)) {
James Hogandfc33382010-04-05 11:31:29 +010055 flags = WDIOS_DISABLECARD;
56 ioctl(fd, WDIOC_SETOPTIONS, &flags);
Randy Dunlap56fb9e52006-05-21 20:58:10 -070057 fprintf(stderr, "Watchdog card disabled.\n");
58 fflush(stderr);
Devendra Naga3c2a6182012-05-14 23:42:02 +053059 goto end;
Randy Dunlap56fb9e52006-05-21 20:58:10 -070060 } else if (!strncasecmp(argv[1], "-e", 2)) {
James Hogandfc33382010-04-05 11:31:29 +010061 flags = WDIOS_ENABLECARD;
62 ioctl(fd, WDIOC_SETOPTIONS, &flags);
Randy Dunlap56fb9e52006-05-21 20:58:10 -070063 fprintf(stderr, "Watchdog card enabled.\n");
64 fflush(stderr);
Devendra Naga3c2a6182012-05-14 23:42:02 +053065 goto end;
Randy Dunlap56fb9e52006-05-21 20:58:10 -070066 } else {
67 fprintf(stderr, "-d to disable, -e to enable.\n");
68 fprintf(stderr, "run by itself to tick the card.\n");
69 fflush(stderr);
Devendra Naga3c2a6182012-05-14 23:42:02 +053070 goto end;
Randy Dunlap56fb9e52006-05-21 20:58:10 -070071 }
72 } else {
73 fprintf(stderr, "Watchdog Ticking Away!\n");
74 fflush(stderr);
75 }
76
Devendra Nagacad19fa2012-05-17 15:07:48 +053077 signal(SIGINT, term);
78
Randy Dunlap56fb9e52006-05-21 20:58:10 -070079 while(1) {
80 keep_alive();
81 sleep(1);
82 }
Devendra Naga3c2a6182012-05-14 23:42:02 +053083end:
84 close(fd);
85 return 0;
Randy Dunlap56fb9e52006-05-21 20:58:10 -070086}