watchdog.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. USE_WATCHDOG(NEWTOY(watchdog, "<1>1Ft#=4<1T#=60<1", TOYFLAG_NEEDROOT|TOYFLAG_BIN))
  7. config WATCHDOG
  8. bool "watchdog"
  9. default y
  10. help
  11. usage: watchdog [-F] [-t UPDATE] [-T DEADLINE] DEV
  12. Start the watchdog timer at DEV with optional timeout parameters.
  13. -F run in the foreground (do not daemonize)
  14. -t poke watchdog every UPDATE seconds (default 4)
  15. -T reboot if not poked for DEADLINE seconds (default 60)
  16. */
  17. #define FOR_watchdog
  18. #include "toys.h"
  19. #include "linux/watchdog.h"
  20. GLOBALS(
  21. long T, t;
  22. int fd;
  23. )
  24. static void safe_shutdown(int ignored)
  25. {
  26. write(TT.fd, "V", 1);
  27. close(TT.fd);
  28. error_exit("safely exited watchdog.");
  29. }
  30. void watchdog_main(void)
  31. {
  32. if (!FLAG(F)) xvdaemon();
  33. xsignal(SIGTERM, safe_shutdown);
  34. xsignal(SIGINT, safe_shutdown);
  35. xioctl(TT.fd = xopen(*toys.optargs, O_WRONLY), WDIOC_SETTIMEOUT, &TT.T);
  36. // Now that we've got the watchdog device open, kick it periodically.
  37. for (;;) {
  38. write(TT.fd, "", 1);
  39. sleep(TT.t);
  40. }
  41. }