aboutsummaryrefslogtreecommitdiffstats
path: root/src/linux/watchdog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/linux/watchdog.c')
-rw-r--r--src/linux/watchdog.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/linux/watchdog.c b/src/linux/watchdog.c
new file mode 100644
index 00000000..d89fcd29
--- /dev/null
+++ b/src/linux/watchdog.c
@@ -0,0 +1,36 @@
+// Support for Linux watchdog
+//
+// Copyright (C) 2017 Kevin O'Connor <kevin@koconnor.net>
+//
+// This file may be distributed under the terms of the GNU GPLv3 license.
+
+#include <fcntl.h> // open
+#include <unistd.h> // write
+#include "internal.h" // report_errno
+#include "sched.h" // DECL_TASK
+
+static int watchdog_fd = -1;
+
+int
+watchdog_setup(void)
+{
+ int ret = open("/dev/watchdog", O_RDWR|O_CLOEXEC);
+ if (ret < 0) {
+ report_errno("watchdog open", ret);
+ return -1;
+ }
+ watchdog_fd = ret;
+ return set_non_blocking(watchdog_fd);
+}
+
+void
+watchdog_task(void)
+{
+ static struct timespec next_watchdog_time;
+ if (watchdog_fd < 0 || !timer_check_periodic(&next_watchdog_time))
+ return;
+ int ret = write(watchdog_fd, ".", 1);
+ if (ret <= 0)
+ report_errno("watchdog write", ret);
+}
+DECL_TASK(watchdog_task);