Add suppport for retrieving machine name

This adds support for reading /etc/os-release, parsing out
OPENBMC_TARGET_MACHINE and returning this to the caller.

Change-Id: If2a419b9a77597686f5137efce97b1150142f181
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/handler.cpp b/handler.cpp
index 4605e2f..028c97b 100644
--- a/handler.cpp
+++ b/handler.cpp
@@ -33,6 +33,7 @@
 #include <sdbusplus/bus.hpp>
 #include <sstream>
 #include <string>
+#include <string_view>
 #include <tuple>
 #include <xyz/openbmc_project/Common/error.hpp>
 
@@ -233,6 +234,47 @@
     return entityName;
 }
 
+std::string Handler::getMachineName()
+{
+    const char* path = "/etc/os-release";
+    std::ifstream ifs(path);
+    if (ifs.fail())
+    {
+        std::fprintf(stderr, "Failed to open: %s\n", path);
+        throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
+    }
+
+    std::string line;
+    while (true)
+    {
+        std::getline(ifs, line);
+        if (ifs.eof())
+        {
+            std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
+                         path);
+            throw IpmiException(IPMI_CC_INVALID);
+        }
+        if (ifs.fail())
+        {
+            std::fprintf(stderr, "Failed to read: %s\n", path);
+            throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR);
+        }
+        std::string_view lineView(line);
+        constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
+        if (lineView.substr(0, prefix.size()) != prefix)
+        {
+            continue;
+        }
+        lineView.remove_prefix(prefix.size());
+        lineView.remove_prefix(
+            std::min(lineView.find_first_not_of('"'), lineView.size()));
+        lineView.remove_suffix(
+            lineView.size() - 1 -
+            std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
+        return std::string(lineView);
+    }
+}
+
 std::string readNameFromConfig(const std::string& type, uint8_t instance,
                                const Json& config)
 {