aboutsummaryrefslogtreecommitdiff
path: root/toys/other/watchdog.c
blob: 0402d3ee6fab956b711c2a275ee20555407872f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* 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
  help
    usage: watchdog [-F] [-t UPDATE] [-T DEADLINE] DEV

    Start the watchdog timer at DEV with optional timeout parameters.

    -F	run in the foreground (do not daemonize)
    -t	poke watchdog every UPDATE seconds (default 4)
    -T	reboot if not poked for DEADLINE seconds (default 60)
*/

#define FOR_watchdog
#include "toys.h"
#include "linux/watchdog.h"

GLOBALS(
  long T, t;

  int fd;
)

static 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)) xvdaemon();
  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);
  }
}