• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* watchdog - start a watchdog timer with configurable kick frequencies
2  *
3  * Copyright 2019 Chris Sarra <chrissarra@google.com>
4  *
5  * See kernel.org/doc/Documentation/watchdog/watchdog-api.txt
6 
7 USE_WATCHDOG(NEWTOY(watchdog, "<1>1Ft#=4<1T#=60<1", TOYFLAG_NEEDROOT|TOYFLAG_BIN))
8 
9 config WATCHDOG
10   bool "watchdog"
11   default y
12   help
13     usage: watchdog [-F] [-t UPDATE] [-T DEADLINE] DEV
14 
15     Start the watchdog timer at DEV with optional timeout parameters.
16 
17     -F	run in the foreground (do not daemonize)
18     -t	poke watchdog every UPDATE seconds (default 4)
19     -T	reboot if not poked for DEADLINE seconds (default 60)
20 */
21 
22 #define FOR_watchdog
23 #include "toys.h"
24 #include "linux/watchdog.h"
25 
GLOBALS(long T,t;int fd;)26 GLOBALS(
27   long T, t;
28 
29   int fd;
30 )
31 
32 static void safe_shutdown(int ignored)
33 {
34   write(TT.fd, "V", 1);
35   close(TT.fd);
36   error_exit("safely exited watchdog.");
37 }
38 
watchdog_main(void)39 void watchdog_main(void)
40 {
41   if (!FLAG(F)) xvdaemon();
42   xsignal(SIGTERM, safe_shutdown);
43   xsignal(SIGINT, safe_shutdown);
44   xioctl(TT.fd = xopen(*toys.optargs, O_WRONLY), WDIOC_SETTIMEOUT, &TT.T);
45 
46   // Now that we've got the watchdog device open, kick it periodically.
47   for (;;) {
48     write(TT.fd, "", 1);
49     sleep(TT.t);
50   }
51 }
52