diff options
author | Rob Landley <rob@landley.net> | 2020-10-21 01:17:35 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2020-10-21 01:17:35 -0500 |
commit | e8ff755cba9e249d0e43212acbc0c3bf38af1622 (patch) | |
tree | dabdf08d92f5505b213a15307692ded5c6640cf0 /toys/other | |
parent | 9c8b40514fc3adfe76cc60fe183ad162adbd327e (diff) | |
download | toybox-e8ff755cba9e249d0e43212acbc0c3bf38af1622.tar.gz |
Promote watchdog.
Diffstat (limited to 'toys/other')
-rw-r--r-- | toys/other/watchdog.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/toys/other/watchdog.c b/toys/other/watchdog.c new file mode 100644 index 00000000..7a49c0b0 --- /dev/null +++ b/toys/other/watchdog.c @@ -0,0 +1,50 @@ +/* watchdog - start a watchdog timer with configurable kick frequencies + * + * Copyright 2019 Chris Sarra <chrissarra@google.com> + * + * See kernel.org/doc/Documentation/watchdog/watchdog-api.txt + +USE_WATCHDOG(NEWTOY(watchdog, "<1>1Ft#=4<1T#=60<1", TOYFLAG_NEEDROOT|TOYFLAG_BIN)) + +config WATCHDOG + bool "watchdog" + default y + depends on TOYBOX_FORK + help + usage: watchdog [-F] [-t SW_TIMER_S] [-T HW_TIMER_S] DEV + + Start the watchdog timer at DEV with optional timeout parameters. + + -F run in the foreground (do not daemonize) + -t software timer (in seconds) + -T hardware timer (in seconds) +*/ +#define FOR_watchdog +#include "toys.h" +#include "linux/watchdog.h" + +GLOBALS( + long T, t; + + int fd; +) + +void safe_shutdown(int ignored) { + write(TT.fd, "V", 1); + close(TT.fd); + error_exit("safely exited watchdog."); +} + +void watchdog_main(void) +{ + if (!FLAG(F) && daemon(1, 1)) perror_exit("failed to daemonize"); + xsignal(SIGTERM, safe_shutdown); + xsignal(SIGINT, safe_shutdown); + xioctl(TT.fd = xopen(*toys.optargs, O_WRONLY), WDIOC_SETTIMEOUT, &TT.T); + + // Now that we've got the watchdog device open, kick it periodically. + for (;;) { + write(TT.fd, "", 1); + sleep(TT.t); + } +} |