watchdog: add support for systemd watchdog
Add interfaces and handling for systemd watchdog petting.
Systemd service files can specify a watchdog timeout and systemd will
expect the application to periodically poke a software watchdog, or
else the service will be restarted. This is enabled with the
'WatchdogSec=' service file directive. Add primitives for
interacting with the watchdog APIs.
Enable automatic support in the `async::context` for this watchdog
handling, such that if the watchdog is required (by checking
sd_watchdog_enabled) the daemon will automatically pet at the
appropriate rate, assuming that the `async::context` is functioning
correctly.
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I68caf7b2c7166ca402b07ecee3db65f75365aa72
diff --git a/src/bus.cpp b/src/bus.cpp
index 95dfc61..5d542c8 100644
--- a/src/bus.cpp
+++ b/src/bus.cpp
@@ -141,4 +141,33 @@
}
}
+void bus::watchdog_pet()
+{
+ int r = _intf->sd_notify(0, "WATCHDOG=1");
+ if (r < 0)
+ {
+ throw exception::SdBusError(-r, "sd_notify WATCHDOG=1");
+ }
+}
+
+void bus::watchdog_trigger()
+{
+ int r = _intf->sd_notify(0, "WATCHDOG=trigger");
+ if (r < 0)
+ {
+ throw exception::SdBusError(-r, "sd_notify WATCHDOG=trigger");
+ }
+}
+
+uint64_t bus::watchdog_enabled()
+{
+ uint64_t usec = 0;
+ int r = _intf->sd_watchdog_enabled(0, &usec);
+ if (r < 0)
+ {
+ throw exception::SdBusError(-r, "sd_watchdog_enabled");
+ }
+ return usec;
+}
+
} // namespace sdbusplus::bus