1
2
3
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <unistd.h>
9#include <fcntl.h>
10#include <signal.h>
11#include <sys/ioctl.h>
12#include <linux/types.h>
13#include <linux/watchdog.h>
14
15int fd;
16
17
18
19
20
21
22static void keep_alive(void)
23{
24 int dummy;
25
26 ioctl(fd, WDIOC_KEEPALIVE, &dummy);
27}
28
29
30
31
32
33
34static void term(int sig)
35{
36 close(fd);
37 fprintf(stderr, "Stopping watchdog ticks...\n");
38 exit(0);
39}
40
41int main(int argc, char *argv[])
42{
43 int flags;
44
45 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)) {
55 flags = WDIOS_DISABLECARD;
56 ioctl(fd, WDIOC_SETOPTIONS, &flags);
57 fprintf(stderr, "Watchdog card disabled.\n");
58 fflush(stderr);
59 goto end;
60 } else if (!strncasecmp(argv[1], "-e", 2)) {
61 flags = WDIOS_ENABLECARD;
62 ioctl(fd, WDIOC_SETOPTIONS, &flags);
63 fprintf(stderr, "Watchdog card enabled.\n");
64 fflush(stderr);
65 goto end;
66 } 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);
70 goto end;
71 }
72 } else {
73 fprintf(stderr, "Watchdog Ticking Away!\n");
74 fflush(stderr);
75 }
76
77 signal(SIGINT, term);
78
79 while(1) {
80 keep_alive();
81 sleep(1);
82 }
83end:
84 close(fd);
85 return 0;
86}
87