From 418ba126a955ee38804152c6c905ff200bbc1a92 Mon Sep 17 00:00:00 2001
From: José Bollo <jose.bollo@iot.bzh>
Date: Wed, 13 Feb 2019 17:09:05 +0100
Subject: watchdog: Isolate the watchdog from jobs
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Change-Id: Iaa7f71dc7e5d8d525463619b4da980c827722909
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
---
 src/CMakeLists.txt        |  1 +
 src/jobs.c                | 15 ---------------
 src/main-afb-daemon.c     |  8 ++++++++
 src/main-afs-supervisor.c |  9 +++++++++
 src/watchdog.c            | 39 +++++++++++++++++++++++++++++++++++++++
 src/watchdog.h            | 31 +++++++++++++++++++++++++++++++
 6 files changed, 88 insertions(+), 15 deletions(-)
 create mode 100644 src/watchdog.c
 create mode 100644 src/watchdog.h

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 47332687..086a2aa1 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -75,6 +75,7 @@ SET(AFB_LIB_SOURCES
 	sig-monitor.c
 	subpath.c
 	verbose.c
+	watchdog.c
 	websock.c
 	wrap-json.c
 )
diff --git a/src/jobs.c b/src/jobs.c
index abfe699e..f30904a8 100644
--- a/src/jobs.c
+++ b/src/jobs.c
@@ -17,12 +17,6 @@
 
 #define _GNU_SOURCE
 
-#if defined(NO_JOBS_WATCHDOG)
-#   define HAS_WATCHDOG 0
-#else
-#   define HAS_WATCHDOG 1
-#endif
-
 #include <stdlib.h>
 #include <stdint.h>
 #include <unistd.h>
@@ -36,9 +30,6 @@
 #include <sys/eventfd.h>
 
 #include <systemd/sd-event.h>
-#if HAS_WATCHDOG
-#include <systemd/sd-daemon.h>
-#endif
 
 #include "jobs.h"
 #include "sig-monitor.h"
@@ -890,12 +881,6 @@ int jobs_start(int allowed_count, int start_count, int waiter_count, void (*star
 	running = 0;
 	remains = waiter_count;
 
-#if HAS_WATCHDOG
-	/* set the watchdog */
-	if (sd_watchdog_enabled(0, NULL))
-		sd_event_set_watchdog(get_sd_event_locked(), 1);
-#endif
-
 	/* start at least one thread: the current one */
 	launched = 1;
 	while (launched < start_count) {
diff --git a/src/main-afb-daemon.c b/src/main-afb-daemon.c
index 0a8e0186..beb08d38 100644
--- a/src/main-afb-daemon.c
+++ b/src/main-afb-daemon.c
@@ -66,6 +66,7 @@
 #include "wrap-json.h"
 #include "jobs.h"
 #include "sig-monitor.h"
+#include "watchdog.h"
 
 #if !defined(DEFAULT_BINDER_INTERFACE)
 #  define DEFAULT_BINDER_INTERFACE NULL
@@ -861,6 +862,13 @@ static void start(int signum, void *arg)
 
 	/* ready */
 	sd_notify(1, "READY=1");
+
+	/* activate the watchdog */
+#if HAS_WATCHDOG
+	if (watchdog_activate() < 0)
+		ERROR("can't start the watchdog");
+#endif
+
 	return;
 error:
 	exit(1);
diff --git a/src/main-afs-supervisor.c b/src/main-afs-supervisor.c
index b4250831..6b79c9c1 100644
--- a/src/main-afs-supervisor.c
+++ b/src/main-afs-supervisor.c
@@ -39,6 +39,7 @@
 #include "verbose.h"
 #include "jobs.h"
 #include "process-name.h"
+#include "watchdog.h"
 
 #if !defined(DEFAULT_SUPERVISOR_INTERFACE)
 #  define DEFAULT_SUPERVISOR_INTERFACE NULL
@@ -191,6 +192,14 @@ static void start(int signum, void *arg)
 
 	/* ready */
 	sd_notify(1, "READY=1");
+
+	/* activate the watchdog */
+#if HAS_WATCHDOG
+	if (watchdog_activate() < 0)
+		ERROR("can't start the watchdog");
+#endif
+
+	/* discover binders */
 	afs_supervisor_discover();
 	return;
 error:
diff --git a/src/watchdog.c b/src/watchdog.c
new file mode 100644
index 00000000..f9afd640
--- /dev/null
+++ b/src/watchdog.c
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2016-2019 "IoT.bzh"
+ * Author José Bollo <jose.bollo@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define _GNU_SOURCE
+
+#include "watchdog.h"
+
+#if HAS_WATCHDOG
+
+#include <stdlib.h>
+
+#include <systemd/sd-event.h>
+#include <systemd/sd-daemon.h>
+
+#include "jobs.h"
+
+int watchdog_activate()
+{
+	/* set the watchdog */
+	if (sd_watchdog_enabled(0, NULL))
+		sd_event_set_watchdog(jobs_get_sd_event(), 1);
+	return 0;
+}
+
+#endif
\ No newline at end of file
diff --git a/src/watchdog.h b/src/watchdog.h
new file mode 100644
index 00000000..6dc0a054
--- /dev/null
+++ b/src/watchdog.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2016-2019 "IoT.bzh"
+ * Author José Bollo <jose.bollo@iot.bzh>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#if !defined(HAS_WATCHDOG)
+#  if defined(NO_JOBS_WATCHDOG)
+#     define HAS_WATCHDOG 0
+#  else
+#     define HAS_WATCHDOG 1
+#  endif
+#endif
+
+#if HAS_WATCHDOG
+extern int watchdog_activate();
+#endif
+
-- 
cgit