Allow initiating or offloading host dumps only when host is up.

When the host is not up the system dump cannot be initiated or
offloaded. Adding a check whether the host is up before starting
the creation of the host dump.
Added a new utility to check whether host is running, this can
be called to check whether host is running before initiating
actions like initiate, offload, delete on host dumps.
Automatic clearing off host dump entries during a system power
off will be handled with #ibm-openbmc/issues/2860

Signed-off-by: Dhruvaraj Subhashchandran <dhruvaraj@in.ibm.com>
Change-Id: Ieb3163ef9b329d3b99807722bd5ac985bf3da25e
diff --git a/dump-extensions/openpower-dumps/system_dump_entry.cpp b/dump-extensions/openpower-dumps/system_dump_entry.cpp
index eed76bd..970a03c 100644
--- a/dump-extensions/openpower-dumps/system_dump_entry.cpp
+++ b/dump-extensions/openpower-dumps/system_dump_entry.cpp
@@ -3,15 +3,30 @@
 #include "dump_utils.hpp"
 #include "host_transport_exts.hpp"
 
+#include <phosphor-logging/elog-errors.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
 namespace phosphor
 {
 namespace dump
 {
 namespace system
 {
+using namespace phosphor::logging;
 
 void Entry::initiateOffload(std::string uri)
 {
+    using NotAllowed =
+        sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
+    using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
+
+    // Allow offloading only when the host is up.
+    if (!phosphor::dump::isHostRunning())
+    {
+        elog<NotAllowed>(
+            Reason("System dump can be offloaded only when the host is up"));
+        return;
+    }
     phosphor::dump::Entry::initiateOffload(uri);
     phosphor::dump::host::requestOffload(sourceDumpId());
 }
@@ -25,10 +40,7 @@
 
     // Remove host system dump when host is up by using source dump id
     // which is present in system dump entry dbus object as a property.
-    BootProgress bootProgressStatus = phosphor::dump::getBootProgress();
-    if ((bootProgressStatus == BootProgress::SystemInitComplete) ||
-        (bootProgressStatus == BootProgress::OSStart) ||
-        (bootProgressStatus == BootProgress::OSRunning))
+    if (phosphor::dump::isHostRunning())
     {
         phosphor::dump::host::requestDelete(srcDumpID);
     }