LayerManagerService: added systemd health monitor
authorTimo Lotterbach <timo.lotterbach@bmw-carit.de>
Tue, 20 Nov 2012 11:04:53 +0000 (03:04 -0800)
committerTimo Lotterbach <timo.lotterbach@bmw-carit.de>
Tue, 20 Nov 2012 11:57:50 +0000 (03:57 -0800)
the systemd health monitor supports control of the systemd watchdog

Signed-off-by: Timo Lotterbach <timo.lotterbach@bmw-carit.de>
LayerManagerService/CMakeLists.txt
LayerManagerService/include/HealthSystemd.h [new file with mode: 0644]
LayerManagerService/src/HealthSystemd.cpp [new file with mode: 0644]

index 20ba91e..d26c2d2 100644 (file)
@@ -79,6 +79,7 @@ add_executable(LayerManagerService_Test
     src/GraphicalSurface.cpp
     src/Layermanager.cpp
     src/InputManager.cpp
+    src/HealthSystemd.cpp
     src/Scene.cpp
     src/shader/Shader.cpp
     src/shader/ShaderProgram.cpp
diff --git a/LayerManagerService/include/HealthSystemd.h b/LayerManagerService/include/HealthSystemd.h
new file mode 100644 (file)
index 0000000..f227cf1
--- /dev/null
@@ -0,0 +1,39 @@
+/***************************************************************************
+*
+* Copyright 2012 BMW Car IT GmbH
+*
+*
+* 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.
+*
+****************************************************************************/
+
+#ifndef __HEALTHSYSTEMD_H__
+#define __HEALTHSYSTEMD_H__
+
+#include "IHealth.h"
+
+class HealthSystemd : public IHealth
+{
+public:
+    HealthSystemd();
+    virtual void reportStartupComplete();
+    virtual void reportProcessId();
+    virtual int getWatchdogIntervalInMs();
+    virtual void signalWatchdog();
+    virtual bool watchdogEnabled();
+
+private:
+    int mIntervalInMs;
+};
+
+#endif // __HEALTHSYSTEMD_H__
\ No newline at end of file
diff --git a/LayerManagerService/src/HealthSystemd.cpp b/LayerManagerService/src/HealthSystemd.cpp
new file mode 100644 (file)
index 0000000..b393e40
--- /dev/null
@@ -0,0 +1,80 @@
+/***************************************************************************
+*
+* Copyright 2012 BMW Car IT GmbH
+*
+*
+* 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.
+*
+****************************************************************************/
+#include "HealthSystemd.h"
+#include "config.h"
+#include "Log.h"
+#include <unistd.h>
+#include <stdlib.h>
+
+#ifdef WITH_SYSTEMD
+    #include <systemd/sd-daemon.h>
+#else
+    #define sd_notify(a,b)
+    #define sd_notifyf(a,b,c)
+#endif
+
+
+HealthSystemd::HealthSystemd()
+: mIntervalInMs(-1)
+{
+    char* envVar = getenv("WATCHDOG_USEC");
+    if (envVar)
+    {
+        // to ms, watchdog should trigger twice the frequency
+        mIntervalInMs = atoi(envVar) / 2000;
+        LOG_INFO("HealthSystemd", "Watchdog interval is " << mIntervalInMs << "ms");
+    }
+    else
+    {
+        LOG_INFO("HealthSystemd", "Watchdog interval not defined, watchdog disabled");
+    }
+}
+
+void HealthSystemd::reportStartupComplete()
+{
+    LOG_INFO("HealthSystemd", "reporting startup complete");
+    sd_notify(0, "READY=1");
+}
+
+void HealthSystemd::reportProcessId()
+{
+    if (watchdogEnabled())
+    {
+        sd_notifyf(0, "MAINPID=%d", getpid());
+    }
+}
+
+int HealthSystemd::getWatchdogIntervalInMs()
+{
+    return mIntervalInMs;
+}
+
+void HealthSystemd::signalWatchdog()
+{
+    if (watchdogEnabled())
+    {
+        LOG_DEBUG("HealthSystemd", "Watchdog fired");
+        sd_notify(0, "WATCHDOG=1");
+    }
+}
+
+bool HealthSystemd::watchdogEnabled()
+{
+    return (mIntervalInMs > 0);
+}