bmc-reset: move to quiesce if host was running

If the host was booting prior to the BMC reboot and it is no longer
responding, move the host to Quiesce state so recovery actions can be
taken.

Signed-off-by: Andrew Geissler <geissonator@yahoo.com>
Change-Id: I0e441c5932628ee386ad1c7d9d924ff3dd5bd6d9
diff --git a/host_reset_recovery.cpp b/host_reset_recovery.cpp
index 529e298..f138c3c 100644
--- a/host_reset_recovery.cpp
+++ b/host_reset_recovery.cpp
@@ -23,6 +23,11 @@
 constexpr auto LOGGING_PATH = "/xyz/openbmc_project/logging";
 constexpr auto LOGGING_CREATE_INTF = "xyz.openbmc_project.Logging.Create";
 
+constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
+constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
+constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
+constexpr auto HOST_STATE_QUIESCE_TGT = "obmc-host-quiesce@0.target";
+
 using namespace phosphor::logging;
 
 bool wasHostBooting(sdbusplus::bus::bus& bus)
@@ -113,6 +118,28 @@
     return !f.good();
 }
 
+void moveToHostQuiesce(sdbusplus::bus::bus& bus)
+{
+    try
+    {
+        auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
+                                          SYSTEMD_INTERFACE, "StartUnit");
+
+        method.append(HOST_STATE_QUIESCE_TGT);
+        method.append("replace");
+
+        bus.call_noreply(method);
+    }
+    catch (const sdbusplus::exception::exception& e)
+    {
+        log<level::ERR>("sdbusplus call exception starting quiesce target",
+                        entry("EXCEPTION=%s", e.what()));
+
+        throw std::runtime_error(
+            "Error in invoking D-Bus systemd StartUnit method");
+    }
+}
+
 int main()
 {
 
@@ -143,8 +170,7 @@
     // Host was booting before the BMC reboot so log an error and go to host
     // quiesce target
     createErrorLog(bus);
-
-    // TODO Move to Host Quiesce
+    moveToHostQuiesce(bus);
 
     return 0;
 }