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:Tn:NLf:i";
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 {"gettimeout", no_argument
, NULL
, 'T'},
31 {"pretimeout", required_argument
, NULL
, 'n'},
32 {"getpretimeout", no_argument
, NULL
, 'N'},
33 {"gettimeleft", no_argument
, NULL
, 'L'},
34 {"file", required_argument
, NULL
, 'f'},
35 {"info", no_argument
, NULL
, 'i'},
36 {NULL
, no_argument
, NULL
, 0x0}
40 * This function simply sends an IOCTL to the driver, which in turn ticks
41 * the PC Watchdog card to reset its internal timer so it doesn't trigger
44 static void keep_alive(void)
49 ret
= ioctl(fd
, WDIOC_KEEPALIVE
, &dummy
);
55 * The main program. Run the program with "-d" to disable the card,
56 * or "-e" to enable the card.
59 static void term(int sig
)
61 int ret
= write(fd
, &v
, 1);
65 printf("\nStopping watchdog ticks failed (%d)...\n", errno
);
67 printf("\nStopping watchdog ticks...\n");
71 static void usage(char *progname
)
73 printf("Usage: %s [options]\n", progname
);
74 printf(" -f, --file\t\tOpen watchdog device file\n");
75 printf("\t\t\tDefault is /dev/watchdog\n");
76 printf(" -i, --info\t\tShow watchdog_info\n");
77 printf(" -b, --bootstatus\tGet last boot status (Watchdog/POR)\n");
78 printf(" -d, --disable\t\tTurn off the watchdog timer\n");
79 printf(" -e, --enable\t\tTurn on the watchdog timer\n");
80 printf(" -h, --help\t\tPrint the help message\n");
81 printf(" -p, --pingrate=P\tSet ping rate to P seconds (default %d)\n",
83 printf(" -t, --timeout=T\tSet timeout to T seconds\n");
84 printf(" -T, --gettimeout\tGet the timeout\n");
85 printf(" -n, --pretimeout=T\tSet the pretimeout to T seconds\n");
86 printf(" -N, --getpretimeout\tGet the pretimeout\n");
87 printf(" -L, --gettimeleft\tGet the time left until timer expires\n");
89 printf("Parameters are parsed left-to-right in real-time.\n");
90 printf("Example: %s -d -t 10 -p 5 -e\n", progname
);
91 printf("Example: %s -t 12 -T -n 7 -N\n", progname
);
94 int main(int argc
, char *argv
[])
97 unsigned int ping_rate
= DEFAULT_PING_RATE
;
101 char *file
= "/dev/watchdog";
102 struct watchdog_info info
;
104 setbuf(stdout
, NULL
);
106 while ((c
= getopt_long(argc
, argv
, sopts
, lopts
, NULL
)) != -1) {
111 fd
= open(file
, O_WRONLY
);
115 printf("Watchdog device (%s) not found.\n", file
);
116 else if (errno
== EACCES
)
117 printf("Run watchdog as root.\n");
119 printf("Watchdog device open failed %s\n",
125 * Validate that `file` is a watchdog device
127 ret
= ioctl(fd
, WDIOC_GETSUPPORT
, &info
);
129 printf("WDIOC_GETSUPPORT error '%s'\n", strerror(errno
));
136 while ((c
= getopt_long(argc
, argv
, sopts
, lopts
, NULL
)) != -1) {
141 ret
= ioctl(fd
, WDIOC_GETBOOTSTATUS
, &flags
);
143 printf("Last boot is caused by: %s.\n", (flags
!= 0) ?
144 "Watchdog" : "Power-On-Reset");
146 printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno
));
149 flags
= WDIOS_DISABLECARD
;
150 ret
= ioctl(fd
, WDIOC_SETOPTIONS
, &flags
);
152 printf("Watchdog card disabled.\n");
154 printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno
));
159 flags
= WDIOS_ENABLECARD
;
160 ret
= ioctl(fd
, WDIOC_SETOPTIONS
, &flags
);
162 printf("Watchdog card enabled.\n");
164 printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno
));
169 ping_rate
= strtoul(optarg
, NULL
, 0);
171 ping_rate
= DEFAULT_PING_RATE
;
172 printf("Watchdog ping rate set to %u seconds.\n", ping_rate
);
175 flags
= strtoul(optarg
, NULL
, 0);
176 ret
= ioctl(fd
, WDIOC_SETTIMEOUT
, &flags
);
178 printf("Watchdog timeout set to %u seconds.\n", flags
);
180 printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno
));
186 ret
= ioctl(fd
, WDIOC_GETTIMEOUT
, &flags
);
188 printf("WDIOC_GETTIMEOUT returns %u seconds.\n", flags
);
190 printf("WDIOC_GETTIMEOUT error '%s'\n", strerror(errno
));
193 flags
= strtoul(optarg
, NULL
, 0);
194 ret
= ioctl(fd
, WDIOC_SETPRETIMEOUT
, &flags
);
196 printf("Watchdog pretimeout set to %u seconds.\n", flags
);
198 printf("WDIOC_SETPRETIMEOUT error '%s'\n", strerror(errno
));
204 ret
= ioctl(fd
, WDIOC_GETPRETIMEOUT
, &flags
);
206 printf("WDIOC_GETPRETIMEOUT returns %u seconds.\n", flags
);
208 printf("WDIOC_GETPRETIMEOUT error '%s'\n", strerror(errno
));
212 ret
= ioctl(fd
, WDIOC_GETTIMELEFT
, &flags
);
214 printf("WDIOC_GETTIMELEFT returns %u seconds.\n", flags
);
216 printf("WDIOC_GETTIMELEFT error '%s'\n", strerror(errno
));
223 * watchdog_info was obtained as part of file open
224 * validation. So we just show it here.
227 printf("watchdog_info:\n");
228 printf(" identity:\t\t%s\n", info
.identity
);
229 printf(" firmware_version:\t%u\n",
230 info
.firmware_version
);
231 printf(" options:\t\t%08x\n", info
.options
);
243 printf("Watchdog Ticking Away!\n");
245 signal(SIGINT
, term
);
252 ret
= write(fd
, &v
, 1);
254 printf("Stopping watchdog ticks failed (%d)...\n", errno
);