1 // SPDX-License-Identifier: GPL-2.0
3 * Watchdog Driver Test Program
5 * - Tests Magic Close - CONFIG_WATCHDOG_NOWAYOUT
6 * - Could be tested against softdog driver on systems that
7 * don't have watchdog hardware.
9 * - Enhance test to add coverage for WDIOC_GETTEMP.
11 * Reference: Documentation/watchdog/watchdog-api.rst
22 #include <sys/ioctl.h>
23 #include <linux/types.h>
24 #include <linux/watchdog.h>
26 #define DEFAULT_PING_RATE 1
30 static const char sopts
[] = "bdehp:st:Tn:NLf:i";
31 static const struct option lopts
[] = {
32 {"bootstatus", no_argument
, NULL
, 'b'},
33 {"disable", no_argument
, NULL
, 'd'},
34 {"enable", no_argument
, NULL
, 'e'},
35 {"help", no_argument
, NULL
, 'h'},
36 {"pingrate", required_argument
, NULL
, 'p'},
37 {"status", no_argument
, NULL
, 's'},
38 {"timeout", required_argument
, NULL
, 't'},
39 {"gettimeout", no_argument
, NULL
, 'T'},
40 {"pretimeout", required_argument
, NULL
, 'n'},
41 {"getpretimeout", no_argument
, NULL
, 'N'},
42 {"gettimeleft", no_argument
, NULL
, 'L'},
43 {"file", required_argument
, NULL
, 'f'},
44 {"info", no_argument
, NULL
, 'i'},
45 {NULL
, no_argument
, NULL
, 0x0}
49 * This function simply sends an IOCTL to the driver, which in turn ticks
50 * the PC Watchdog card to reset its internal timer so it doesn't trigger
53 static void keep_alive(void)
58 ret
= ioctl(fd
, WDIOC_KEEPALIVE
, &dummy
);
64 * The main program. Run the program with "-d" to disable the card,
65 * or "-e" to enable the card.
68 static void term(int sig
)
70 int ret
= write(fd
, &v
, 1);
74 printf("\nStopping watchdog ticks failed (%d)...\n", errno
);
76 printf("\nStopping watchdog ticks...\n");
80 static void usage(char *progname
)
82 printf("Usage: %s [options]\n", progname
);
83 printf(" -f, --file\t\tOpen watchdog device file\n");
84 printf("\t\t\tDefault is /dev/watchdog\n");
85 printf(" -i, --info\t\tShow watchdog_info\n");
86 printf(" -s, --status\t\tGet status & supported features\n");
87 printf(" -b, --bootstatus\tGet last boot status (Watchdog/POR)\n");
88 printf(" -d, --disable\t\tTurn off the watchdog timer\n");
89 printf(" -e, --enable\t\tTurn on the watchdog timer\n");
90 printf(" -h, --help\t\tPrint the help message\n");
91 printf(" -p, --pingrate=P\tSet ping rate to P seconds (default %d)\n",
93 printf(" -t, --timeout=T\tSet timeout to T seconds\n");
94 printf(" -T, --gettimeout\tGet the timeout\n");
95 printf(" -n, --pretimeout=T\tSet the pretimeout to T seconds\n");
96 printf(" -N, --getpretimeout\tGet the pretimeout\n");
97 printf(" -L, --gettimeleft\tGet the time left until timer expires\n");
99 printf("Parameters are parsed left-to-right in real-time.\n");
100 printf("Example: %s -d -t 10 -p 5 -e\n", progname
);
101 printf("Example: %s -t 12 -T -n 7 -N\n", progname
);
104 struct wdiof_status
{
106 const char *status_str
;
109 #define WDIOF_NUM_STATUS 8
111 static const struct wdiof_status wdiof_status
[WDIOF_NUM_STATUS
] = {
112 {WDIOF_SETTIMEOUT
, "Set timeout (in seconds)"},
113 {WDIOF_MAGICCLOSE
, "Supports magic close char"},
114 {WDIOF_PRETIMEOUT
, "Pretimeout (in seconds), get/set"},
115 {WDIOF_ALARMONLY
, "Watchdog triggers a management or other external alarm not a reboot"},
116 {WDIOF_KEEPALIVEPING
, "Keep alive ping reply"},
117 {WDIOS_DISABLECARD
, "Turn off the watchdog timer"},
118 {WDIOS_ENABLECARD
, "Turn on the watchdog timer"},
119 {WDIOS_TEMPPANIC
, "Kernel panic on temperature trip"},
122 static void print_status(int flags
)
126 if (flags
== WDIOS_UNKNOWN
) {
127 printf("Unknown status error from WDIOC_GETSTATUS\n");
131 for (wdiof
= 0; wdiof
< WDIOF_NUM_STATUS
; wdiof
++) {
132 if (flags
& wdiof_status
[wdiof
].flag
)
133 printf("Support/Status: %s\n",
134 wdiof_status
[wdiof
].status_str
);
138 #define WDIOF_NUM_BOOTSTATUS 7
140 static const struct wdiof_status wdiof_bootstatus
[WDIOF_NUM_BOOTSTATUS
] = {
141 {WDIOF_OVERHEAT
, "Reset due to CPU overheat"},
142 {WDIOF_FANFAULT
, "Fan failed"},
143 {WDIOF_EXTERN1
, "External relay 1"},
144 {WDIOF_EXTERN2
, "External relay 2"},
145 {WDIOF_POWERUNDER
, "Power bad/power fault"},
146 {WDIOF_CARDRESET
, "Card previously reset the CPU"},
147 {WDIOF_POWEROVER
, "Power over voltage"},
150 static void print_boot_status(int flags
)
154 if (flags
== WDIOF_UNKNOWN
) {
155 printf("Unknown flag error from WDIOC_GETBOOTSTATUS\n");
160 printf("Last boot is caused by: Power-On-Reset\n");
164 for (wdiof
= 0; wdiof
< WDIOF_NUM_BOOTSTATUS
; wdiof
++) {
165 if (flags
& wdiof_bootstatus
[wdiof
].flag
)
166 printf("Last boot is caused by: %s\n",
167 wdiof_bootstatus
[wdiof
].status_str
);
171 int main(int argc
, char *argv
[])
174 unsigned int ping_rate
= DEFAULT_PING_RATE
;
178 char *file
= "/dev/watchdog";
179 struct watchdog_info info
;
182 setbuf(stdout
, NULL
);
184 while ((c
= getopt_long(argc
, argv
, sopts
, lopts
, NULL
)) != -1) {
189 fd
= open(file
, O_WRONLY
);
193 printf("Watchdog device (%s) not found.\n", file
);
194 else if (errno
== EACCES
)
195 printf("Run watchdog as root.\n");
197 printf("Watchdog device open failed %s\n",
203 * Validate that `file` is a watchdog device
205 ret
= ioctl(fd
, WDIOC_GETSUPPORT
, &info
);
207 printf("WDIOC_GETSUPPORT error '%s'\n", strerror(errno
));
214 while ((c
= getopt_long(argc
, argv
, sopts
, lopts
, NULL
)) != -1) {
219 ret
= ioctl(fd
, WDIOC_GETBOOTSTATUS
, &flags
);
221 print_boot_status(flags
);
223 printf("WDIOC_GETBOOTSTATUS error '%s'\n", strerror(errno
));
226 flags
= WDIOS_DISABLECARD
;
227 ret
= ioctl(fd
, WDIOC_SETOPTIONS
, &flags
);
229 printf("Watchdog card disabled.\n");
231 printf("WDIOS_DISABLECARD error '%s'\n", strerror(errno
));
236 flags
= WDIOS_ENABLECARD
;
237 ret
= ioctl(fd
, WDIOC_SETOPTIONS
, &flags
);
239 printf("Watchdog card enabled.\n");
241 printf("WDIOS_ENABLECARD error '%s'\n", strerror(errno
));
246 ping_rate
= strtoul(optarg
, NULL
, 0);
248 ping_rate
= DEFAULT_PING_RATE
;
249 printf("Watchdog ping rate set to %u seconds.\n", ping_rate
);
254 ret
= ioctl(fd
, WDIOC_GETSTATUS
, &flags
);
258 printf("WDIOC_GETSTATUS error '%s'\n", strerror(errno
));
259 ret
= ioctl(fd
, WDIOC_GETTEMP
, &temperature
);
261 printf("WDIOC_GETTEMP: '%s'\n", strerror(errno
));
263 printf("Temperature %d\n", temperature
);
267 flags
= strtoul(optarg
, NULL
, 0);
268 ret
= ioctl(fd
, WDIOC_SETTIMEOUT
, &flags
);
270 printf("Watchdog timeout set to %u seconds.\n", flags
);
272 printf("WDIOC_SETTIMEOUT error '%s'\n", strerror(errno
));
278 ret
= ioctl(fd
, WDIOC_GETTIMEOUT
, &flags
);
280 printf("WDIOC_GETTIMEOUT returns %u seconds.\n", flags
);
282 printf("WDIOC_GETTIMEOUT error '%s'\n", strerror(errno
));
285 flags
= strtoul(optarg
, NULL
, 0);
286 ret
= ioctl(fd
, WDIOC_SETPRETIMEOUT
, &flags
);
288 printf("Watchdog pretimeout set to %u seconds.\n", flags
);
290 printf("WDIOC_SETPRETIMEOUT error '%s'\n", strerror(errno
));
296 ret
= ioctl(fd
, WDIOC_GETPRETIMEOUT
, &flags
);
298 printf("WDIOC_GETPRETIMEOUT returns %u seconds.\n", flags
);
300 printf("WDIOC_GETPRETIMEOUT error '%s'\n", strerror(errno
));
304 ret
= ioctl(fd
, WDIOC_GETTIMELEFT
, &flags
);
306 printf("WDIOC_GETTIMELEFT returns %u seconds.\n", flags
);
308 printf("WDIOC_GETTIMELEFT error '%s'\n", strerror(errno
));
315 * watchdog_info was obtained as part of file open
316 * validation. So we just show it here.
319 printf("watchdog_info:\n");
320 printf(" identity:\t\t%s\n", info
.identity
);
321 printf(" firmware_version:\t%u\n",
322 info
.firmware_version
);
323 print_status(info
.options
);
335 printf("Watchdog Ticking Away!\n");
338 * Register the signals
340 signal(SIGINT
, term
);
341 signal(SIGTERM
, term
);
342 signal(SIGKILL
, term
);
343 signal(SIGQUIT
, term
);
351 * Send specific magic character 'V' just in case Magic Close is
352 * enabled to ensure watchdog gets disabled on close.
354 ret
= write(fd
, &v
, 1);
356 printf("Stopping watchdog ticks failed (%d)...\n", errno
);