Diff between db5e3a8a47be1412cd87c712789e88b3d9e21210 and 1971bfe687960ed6b24602acf74879c7e1abc714

Changed Files

File Additions Deletions Status
src/shared/mainloop-notify.c +28 -0 modified

Full Patch

diff --git a/src/shared/mainloop-notify.c b/src/shared/mainloop-notify.c
index fdc2f25..2e3b08f 100644
--- a/src/shared/mainloop-notify.c
+++ b/src/shared/mainloop-notify.c
@@ -37,13 +37,27 @@
 
 #include "mainloop.h"
 #include "mainloop-notify.h"
+#include "timeout.h"
+
+#define WATCHDOG_TRIGGER_FREQ 2
 
 static int notify_fd = -1;
 
+static unsigned int watchdog;
+
+static bool watchdog_callback(void *user_data)
+{
+	mainloop_sd_notify("WATCHDOG=1");
+
+	return true;
+}
+
 void mainloop_notify_init(void)
 {
 	const char *sock;
 	struct sockaddr_un addr;
+	const char *watchdog_usec;
+	int msec;
 
 	sock = getenv("NOTIFY_SOCKET");
 	if (!sock)
@@ -67,7 +81,19 @@ void mainloop_notify_init(void)
 	if (bind(notify_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 		close(notify_fd);
 		notify_fd = -1;
+		return;
 	}
+
+	watchdog_usec = getenv("WATCHDOG_USEC");
+	if (!watchdog_usec)
+		return;
+
+	msec = atoi(watchdog_usec) / 1000;
+	if (msec < 0)
+		return;
+
+	watchdog = timeout_add(msec / WATCHDOG_TRIGGER_FREQ,
+				watchdog_callback, NULL, NULL);
 }
 
 void mainloop_notify_exit(void)
@@ -76,6 +102,8 @@ void mainloop_notify_exit(void)
 		close(notify_fd);
 		notify_fd = -1;
 	}
+
+	timeout_remove(watchdog);
 }
 
 int mainloop_sd_notify(const char *state)