share/mainloop: Add watchdog support
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Mon, 26 Nov 2018 14:16:15 +0000 (16:16 +0200)
committerhimanshu <h.himanshu@samsung.com>
Tue, 21 Jan 2020 13:52:26 +0000 (19:22 +0530)
This adds watchdog notification support by sending "WATCHDOG=1" twice
as frequent as required by WATCHDOG_USEC.

Change-Id: I713883290a03961276d8f981818abbc922018bd0
Signed-off-by: himanshu <h.himanshu@samsung.com>
src/shared/mainloop-notify.c

index fdc2f25..2e3b08f 100644 (file)
 
 #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)