1 // SPDX-License-Identifier: GPL-2.0
3 * Watchdog Driver Test Program
14 #include <sys/ioctl.h>
15 #include <linux/types.h>
16 #include <linux/watchdog.h>
18 #define DEFAULT_PING_RATE 1
22 static const char sopts
[] = "bdehp:t:";
23 static const struct option lopts
[] = {
24 {"bootstatus", no_argument
, NULL
, 'b'},
25 {"disable", no_argument
, NULL
, 'd'},
26 {"enable", no_argument
, NULL
, 'e'},
27 {"help", no_argument
, NULL
, 'h'},
28 {"pingrate", required_argument
, NULL
, 'p'},
29 {"timeout", required_argument
, NULL
, 't'},
30 {NULL
, no_argument
, NULL
, 0x0}
34 * This function simply sends an IOCTL to the driver, which in turn ticks
35 * the PC Watchdog card to reset its internal timer so it doesn't trigger
38 static void keep_alive(void)
43 ret
= ioctl(fd
, WDIOC_KEEPALIVE
, &dummy
);
49 * The main program. Run the program with "-d" to disable the card,
50 * or "-e" to enable the card.
53 static void term(int sig
)
55 int ret
= write(fd
, &v
, 1);
59 printf("\nStopping watchdog ticks failed (%d)...\n", errno
);
61 printf("\nStopping watchdog ticks...\n");
65 static void usage(char *progname
)
67 printf("Usage: %s [options]\n", progname
);
68 printf(" -b, --bootstatus Get last boot status (Watchdog/POR)\n");
69 printf(" -d, --disable Turn off the watchdog timer\n");
70 printf(" -e, --enable Turn on the watchdog timer\n");
71 printf(" -h, --help Print the help message\n");
72 printf(" -p, --pingrate=P Set ping rate to P seconds (default %d)\n", DEFAULT_PING_RATE
);
73 printf(" -t, --timeout=T Set timeout to T seconds\n");
75 printf("Parameters are parsed left-to-right in real-time.\n");
76 printf("Example: %s -d -t 10 -p 5 -e\n", progname
);
79 int main(int argc
, char *argv
[])
82 unsigned int ping_rate
= DEFAULT_PING_RATE
;
89 fd
= open("/dev/watchdog", O_WRONLY
);
92 printf("Watchdog device not enabled.\n");
96 while ((c
= getopt_long(argc
, argv
, sopts
, lopts
, NULL
)) != -1) {
101 ret
= ioctl(fd
, WDIOC_GETBOOTSTATUS
, &flags
);
103 printf("Last boot is caused by: %s.\n", (flags
!= 0) ?
104 "Watchdog" : "Power-On-Reset");
106 printf("WDIOC_GETBOOTSTATUS errno '%s'\n", strerror(errno
));
109 flags
= WDIOS_DISABLECARD
;
110 ret
= ioctl(fd
, WDIOC_SETOPTIONS
, &flags
);
112 printf("Watchdog card disabled.\n");
114 printf("WDIOS_DISABLECARD errno '%s'\n", strerror(errno
));
117 flags
= WDIOS_ENABLECARD
;
118 ret
= ioctl(fd
, WDIOC_SETOPTIONS
, &flags
);
120 printf("Watchdog card enabled.\n");
122 printf("WDIOS_ENABLECARD errno '%s'\n", strerror(errno
));
125 ping_rate
= strtoul(optarg
, NULL
, 0);
127 ping_rate
= DEFAULT_PING_RATE
;
128 printf("Watchdog ping rate set to %u seconds.\n", ping_rate
);
131 flags
= strtoul(optarg
, NULL
, 0);
132 ret
= ioctl(fd
, WDIOC_SETTIMEOUT
, &flags
);
134 printf("Watchdog timeout set to %u seconds.\n", flags
);
136 printf("WDIOC_SETTIMEOUT errno '%s'\n", strerror(errno
));
147 printf("Watchdog Ticking Away!\n");
149 signal(SIGINT
, term
);
156 ret
= write(fd
, &v
, 1);
158 printf("Stopping watchdog ticks failed (%d)...\n", errno
);