Commit | Line | Data |
---|---|---|
56fb9e53 RD |
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> | |
cad19fa6 | 10 | #include <signal.h> |
56fb9e53 RD |
11 | #include <sys/ioctl.h> |
12 | #include <linux/types.h> | |
13 | #include <linux/watchdog.h> | |
14 | ||
15 | int 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 | */ | |
b7ed698c | 22 | static void keep_alive(void) |
56fb9e53 RD |
23 | { |
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 | */ | |
cad19fa6 | 33 | |
4b1c2f41 | 34 | static void term(int sig) |
cad19fa6 DN |
35 | { |
36 | close(fd); | |
37 | fprintf(stderr, "Stopping watchdog ticks...\n"); | |
38 | exit(0); | |
39 | } | |
40 | ||
56fb9e53 RD |
41 | int main(int argc, char *argv[]) |
42 | { | |
dfc33383 | 43 | int flags; |
f15d7114 | 44 | unsigned int ping_rate = 1; |
dfc33383 | 45 | |
56fb9e53 RD |
46 | fd = open("/dev/watchdog", O_WRONLY); |
47 | ||
48 | if (fd == -1) { | |
49 | fprintf(stderr, "Watchdog device not enabled.\n"); | |
50 | fflush(stderr); | |
51 | exit(-1); | |
52 | } | |
53 | ||
54 | if (argc > 1) { | |
55 | if (!strncasecmp(argv[1], "-d", 2)) { | |
dfc33383 JH |
56 | flags = WDIOS_DISABLECARD; |
57 | ioctl(fd, WDIOC_SETOPTIONS, &flags); | |
56fb9e53 RD |
58 | fprintf(stderr, "Watchdog card disabled.\n"); |
59 | fflush(stderr); | |
3c2a6186 | 60 | goto end; |
56fb9e53 | 61 | } else if (!strncasecmp(argv[1], "-e", 2)) { |
dfc33383 JH |
62 | flags = WDIOS_ENABLECARD; |
63 | ioctl(fd, WDIOC_SETOPTIONS, &flags); | |
56fb9e53 RD |
64 | fprintf(stderr, "Watchdog card enabled.\n"); |
65 | fflush(stderr); | |
3c2a6186 | 66 | goto end; |
f15d7114 TT |
67 | } else if (!strncasecmp(argv[1], "-t", 2) && argv[2]) { |
68 | flags = atoi(argv[2]); | |
69 | ioctl(fd, WDIOC_SETTIMEOUT, &flags); | |
70 | fprintf(stderr, "Watchdog timeout set to %u seconds.\n", flags); | |
71 | fflush(stderr); | |
72 | goto end; | |
73 | } else if (!strncasecmp(argv[1], "-p", 2) && argv[2]) { | |
74 | ping_rate = strtoul(argv[2], NULL, 0); | |
75 | fprintf(stderr, "Watchdog ping rate set to %u seconds.\n", ping_rate); | |
76 | fflush(stderr); | |
56fb9e53 | 77 | } else { |
f15d7114 TT |
78 | fprintf(stderr, "-d to disable, -e to enable, -t <n> to set " \ |
79 | "the timeout,\n-p <n> to set the ping rate, and \n"); | |
56fb9e53 RD |
80 | fprintf(stderr, "run by itself to tick the card.\n"); |
81 | fflush(stderr); | |
3c2a6186 | 82 | goto end; |
56fb9e53 | 83 | } |
56fb9e53 RD |
84 | } |
85 | ||
f15d7114 TT |
86 | fprintf(stderr, "Watchdog Ticking Away!\n"); |
87 | fflush(stderr); | |
88 | ||
cad19fa6 DN |
89 | signal(SIGINT, term); |
90 | ||
56fb9e53 RD |
91 | while(1) { |
92 | keep_alive(); | |
f15d7114 | 93 | sleep(ping_rate); |
56fb9e53 | 94 | } |
3c2a6186 DN |
95 | end: |
96 | close(fd); | |
97 | return 0; | |
56fb9e53 | 98 | } |