faultlog: Initial framework for Fault Log

Fault Log is a new type of dump. For details please see
https://github.com/openbmc/docs/blob/master/designs/hw-fault-monitor.md

This commit enables creation and deletion of fault log entries.

Tested: Created and deleted fault log entries by calling the
corresponding D-Bus methods via bmcweb (added extra code in bmcweb for
testing this). Also forced a fault log directory creation error by
changing the code to specify a nonexistent directory path "/abc/def/"
instead of FAULTLOG_DUMP_PATH, and forced a fault log file open error
by creating a directory (manually using mkdir) with the same name as
the file that the fault log manager tried to open.

Signed-off-by: Claire Weinan <cweinan@google.com>
Change-Id: I03d4c19a4c131f7224ac895e404c46b1f566617b
diff --git a/dump_manager_faultlog.cpp b/dump_manager_faultlog.cpp
new file mode 100644
index 0000000..a718472
--- /dev/null
+++ b/dump_manager_faultlog.cpp
@@ -0,0 +1,116 @@
+#include "config.h"
+
+#include "dump_manager_faultlog.hpp"
+
+#include "faultlog_dump_entry.hpp"
+
+#include <fmt/core.h>
+
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <xyz/openbmc_project/Common/File/error.hpp>
+#include <xyz/openbmc_project/Common/error.hpp>
+
+#include <filesystem>
+#include <fstream>
+#include <iostream>
+#include <string>
+
+namespace phosphor
+{
+namespace dump
+{
+namespace faultlog
+{
+
+using namespace phosphor::logging;
+using namespace sdbusplus::xyz::openbmc_project::Common::Error;
+using namespace sdbusplus::xyz::openbmc_project::Common::File::Error;
+using ErrnoOpen = xyz::openbmc_project::Common::File::Open::ERRNO;
+using PathOpen = xyz::openbmc_project::Common::File::Open::PATH;
+
+sdbusplus::message::object_path
+    Manager::createDump(phosphor::dump::DumpCreateParams params)
+{
+    log<level::INFO>("In dump_manager_fault.cpp createDump");
+
+    // Currently we ignore the parameters.
+    // TODO phosphor-debug-collector/issues/22: Check parameter values and
+    // exit early if we don't receive the expected parameters
+    if (params.empty())
+    {
+        log<level::INFO>("No additional parameters received");
+    }
+    else
+    {
+        log<level::INFO>("Got additional parameters");
+    }
+
+    // Get the id
+    auto id = lastEntryId + 1;
+    auto idString = std::to_string(id);
+    auto objPath = std::filesystem::path(baseEntryPath) / idString;
+
+    std::filesystem::path faultLogFilePath(std::string(FAULTLOG_DUMP_PATH) +
+                                           idString);
+    std::ofstream faultLogFile;
+
+    errno = 0;
+
+    faultLogFile.open(faultLogFilePath,
+                      std::ofstream::out | std::fstream::trunc);
+
+    if (faultLogFile.is_open())
+    {
+        log<level::INFO>("faultLogFile is open");
+
+        faultLogFile << "This is faultlog file #" << idString << " at "
+                     << std::string(FAULTLOG_DUMP_PATH) + idString << std::endl;
+
+        faultLogFile.close();
+    }
+    else
+    {
+        log<level::ERR>(fmt::format("Failed to open fault log file at {}, "
+                                    "errno({}), strerror(\"{}\"), "
+                                    "OBJECTPATH({}), ID({})",
+                                    faultLogFilePath.c_str(), errno,
+                                    strerror(errno), objPath.c_str(), id)
+                            .c_str());
+        elog<Open>(ErrnoOpen(errno), PathOpen(objPath.c_str()));
+    }
+
+    try
+    {
+        log<level::INFO>("dump_manager_faultlog.cpp: add faultlog entry");
+
+        uint64_t timestamp =
+            std::chrono::duration_cast<std::chrono::microseconds>(
+                std::chrono::system_clock::now().time_since_epoch())
+                .count();
+
+        entries.insert(std::make_pair(
+            id,
+            std::make_unique<faultlog::Entry>(
+                bus, objPath.c_str(), id, timestamp,
+                std::filesystem::file_size(faultLogFilePath), faultLogFilePath,
+                phosphor::dump::OperationStatus::Completed, *this)));
+    }
+    catch (const std::invalid_argument& e)
+    {
+        log<level::ERR>(fmt::format("Error in creating dump entry, "
+                                    "errormsg({}), OBJECTPATH({}), ID({})",
+                                    e.what(), objPath.c_str(), id)
+                            .c_str());
+        elog<InternalFailure>();
+    }
+
+    lastEntryId++;
+
+    log<level::INFO>("End of dump_manager_faultlog.cpp createDump");
+    return objPath.string();
+}
+
+} // namespace faultlog
+} // namespace dump
+} // namespace phosphor
diff --git a/dump_manager_faultlog.hpp b/dump_manager_faultlog.hpp
new file mode 100644
index 0000000..56b1bd6
--- /dev/null
+++ b/dump_manager_faultlog.hpp
@@ -0,0 +1,87 @@
+#pragma once
+
+#include "dump_manager.hpp"
+
+#include <fmt/core.h>
+
+#include <phosphor-logging/elog-errors.hpp>
+#include <phosphor-logging/elog.hpp>
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server/object.hpp>
+#include <xyz/openbmc_project/Dump/Create/server.hpp>
+
+namespace phosphor
+{
+namespace dump
+{
+namespace faultlog
+{
+
+using namespace phosphor::logging;
+
+using CreateIface = sdbusplus::server::object::object<
+    sdbusplus::xyz::openbmc_project::Dump::server::Create>;
+
+/** @class Manager
+ *  @brief FaultLog Dump manager implementation.
+ */
+class Manager :
+    virtual public CreateIface,
+    virtual public phosphor::dump::Manager
+{
+  public:
+    Manager() = delete;
+    Manager(const Manager&) = default;
+    Manager& operator=(const Manager&) = delete;
+    Manager(Manager&&) = delete;
+    Manager& operator=(Manager&&) = delete;
+    virtual ~Manager() = default;
+
+    /** @brief Constructor to put object onto bus at a dbus path.
+     *  @param[in] bus - Bus to attach to.
+     *  @param[in] path - Path to attach at.
+     *  @param[in] baseEntryPath - Base path for dump entry.
+     *  @param[in] filePath - Path where the dumps are stored.
+     */
+    Manager(sdbusplus::bus::bus& bus, const char* path,
+            const std::string& baseEntryPath, const char* filePath) :
+        CreateIface(bus, path),
+        phosphor::dump::Manager(bus, path, baseEntryPath), dumpDir(filePath)
+    {
+        std::error_code ec;
+
+        std::filesystem::create_directory(FAULTLOG_DUMP_PATH, ec);
+
+        if (ec)
+        {
+            log<level::ERR>(fmt::format("dump_manager_faultlog directory {} "
+                                        "not created. error_code = {} ({})",
+                                        FAULTLOG_DUMP_PATH, ec.value(),
+                                        ec.message())
+                                .c_str());
+        }
+    }
+
+    void restore() override
+    {
+        // TODO phosphor-debug-collector/issues/21: Restore fault log entries
+        // after service restart
+        log<level::INFO>("dump_manager_faultlog restore not implemented");
+    }
+
+    /** @brief Method to create a new fault log dump entry
+     *  @param[in] params - Key-value pair input parameters
+     *
+     *  @return object_path - The path to the new dump entry.
+     */
+    sdbusplus::message::object_path
+        createDump(phosphor::dump::DumpCreateParams params) override;
+
+  private:
+    /** @brief Path to the dump file*/
+    std::string dumpDir;
+};
+
+} // namespace faultlog
+} // namespace dump
+} // namespace phosphor
diff --git a/dump_manager_main.cpp b/dump_manager_main.cpp
index ce858e4..55f4812 100644
--- a/dump_manager_main.cpp
+++ b/dump_manager_main.cpp
@@ -4,6 +4,7 @@
 #include "dump_internal.hpp"
 #include "dump_manager.hpp"
 #include "dump_manager_bmc.hpp"
+#include "dump_manager_faultlog.hpp"
 #include "elog_watch.hpp"
 #include "watch.hpp"
 #include "xyz/openbmc_project/Common/error.hpp"
@@ -78,6 +79,12 @@
                                                    OBJ_INTERNAL);
         dumpMgrList.push_back(std::move(bmcDumpMgr));
 
+        std::unique_ptr<phosphor::dump::faultlog::Manager> faultLogMgr =
+            std::make_unique<phosphor::dump::faultlog::Manager>(
+                bus, FAULTLOG_DUMP_OBJPATH, FAULTLOG_DUMP_OBJ_ENTRY,
+                FAULTLOG_DUMP_PATH);
+        dumpMgrList.push_back(std::move(faultLogMgr));
+
         phosphor::dump::loadExtensions(bus, dumpMgrList);
 
         // Restore dbus objects of all dumps
diff --git a/faultlog_dump_entry.cpp b/faultlog_dump_entry.cpp
new file mode 100644
index 0000000..897d98b
--- /dev/null
+++ b/faultlog_dump_entry.cpp
@@ -0,0 +1,38 @@
+#include "faultlog_dump_entry.hpp"
+
+#include <fmt/core.h>
+
+#include <phosphor-logging/log.hpp>
+
+namespace phosphor
+{
+namespace dump
+{
+namespace faultlog
+{
+using namespace phosphor::logging;
+
+void Entry::delete_()
+{
+    log<level::INFO>("In faultlog_dump_entry.cpp delete_()");
+
+    // Delete Dump file from Permanent location
+    try
+    {
+        std::filesystem::remove(file);
+    }
+    catch (const std::filesystem::filesystem_error& e)
+    {
+        // Log Error message and continue
+        log<level::ERR>(
+            fmt::format("Failed to delete dump file, errormsg({})", e.what())
+                .c_str());
+    }
+
+    // Remove Dump entry D-bus object
+    phosphor::dump::Entry::delete_();
+}
+
+} // namespace faultlog
+} // namespace dump
+} // namespace phosphor
diff --git a/faultlog_dump_entry.hpp b/faultlog_dump_entry.hpp
new file mode 100644
index 0000000..50f97cd
--- /dev/null
+++ b/faultlog_dump_entry.hpp
@@ -0,0 +1,74 @@
+#pragma once
+
+#include "dump_entry.hpp"
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server/object.hpp>
+#include <xyz/openbmc_project/Object/Delete/server.hpp>
+#include <xyz/openbmc_project/Time/EpochTime/server.hpp>
+
+#include <filesystem>
+
+namespace phosphor
+{
+namespace dump
+{
+namespace faultlog
+{
+template <typename T>
+using ServerObject = typename sdbusplus::server::object::object<T>;
+
+using EntryIfaces = sdbusplus::server::object::object<>;
+
+class Manager;
+
+/** @class Entry
+ *  @brief OpenBMC Fault Log Dump Entry implementation.
+ */
+class Entry : virtual public EntryIfaces, virtual public phosphor::dump::Entry
+{
+  public:
+    Entry() = delete;
+    Entry(const Entry&) = delete;
+    Entry& operator=(const Entry&) = delete;
+    Entry(Entry&&) = delete;
+    Entry& operator=(Entry&&) = delete;
+    ~Entry() = default;
+
+    /** @brief Constructor for the Dump Entry Object
+     *  @param[in] bus - Bus to attach to.
+     *  @param[in] objPath - Object path to attach to
+     *  @param[in] dumpId - Dump id.
+     *  @param[in] timeStamp - Dump creation timestamp
+     *             in microseconds since the epoch.
+     *  @param[in] fileSize - Dump file size in bytes.
+     *  @param[in] file - Full path of dump file.
+     *  @param[in] status - status of the dump.
+     *  @param[in] parent - The dump entry's parent.
+     */
+    Entry(sdbusplus::bus::bus& bus, const std::string& objPath, uint32_t dumpId,
+          uint64_t timeStamp, uint64_t fileSize,
+          const std::filesystem::path& file,
+          phosphor::dump::OperationStatus status,
+          phosphor::dump::Manager& parent) :
+        EntryIfaces(bus, objPath.c_str(), EntryIfaces::action::defer_emit),
+        phosphor::dump::Entry(bus, objPath.c_str(), dumpId, timeStamp, fileSize,
+                              status, parent),
+        file(file)
+    {
+        // Emit deferred signal.
+        this->phosphor::dump::faultlog::EntryIfaces::emit_object_added();
+    }
+
+    /** @brief Delete this d-bus object.
+     */
+    void delete_() override;
+
+  private:
+    /** @Dump file path */
+    std::filesystem::path file;
+};
+
+} // namespace faultlog
+} // namespace dump
+} // namespace phosphor
diff --git a/meson.build b/meson.build
index 0825c96..2b3e7f5 100644
--- a/meson.build
+++ b/meson.build
@@ -113,6 +113,15 @@
 conf_data.set('JFFS_CORE_FILE_WORKAROUND', get_option('jffs-workaround').enabled(),
                description : 'Turn on jffs workaround for core file'
              )
+conf_data.set_quoted('FAULTLOG_DUMP_OBJ_ENTRY', get_option('FAULTLOG_DUMP_OBJ_ENTRY'),
+                      description : 'The Fault Log dump entry DBus object path'
+                    )
+conf_data.set_quoted('FAULTLOG_DUMP_OBJPATH', get_option('FAULTLOG_DUMP_OBJPATH'),
+                      description : 'The Fault Log Dump manager Dbus path'
+                    )
+conf_data.set_quoted('FAULTLOG_DUMP_PATH', get_option('FAULTLOG_DUMP_PATH'),
+                      description : 'Directory where fault logs are placed'
+                    )
 
 configure_file(configuration : conf_data,
                output : 'config.h'
@@ -152,7 +161,9 @@
         'watch.cpp',
         'bmc_dump_entry.cpp',
         'dump_utils.cpp',
-        'dump_offload.cpp'
+        'dump_offload.cpp',
+        'dump_manager_faultlog.cpp',
+        'faultlog_dump_entry.cpp',
     ]
 
 phosphor_dump_manager_dependency = [
diff --git a/meson_options.txt b/meson_options.txt
index a4788f9..cef17e4 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -85,6 +85,23 @@
         description : 'Enable Open Power specific dumps'
       )
 
+# Fault log options
+
+option('FAULTLOG_DUMP_PATH', type : 'string',
+        value : '/var/lib/phosphor-debug-collector/faultlogs/',
+        description : 'Directory where fault logs are placed'
+      )
+
+option('FAULTLOG_DUMP_OBJPATH', type : 'string',
+        value : '/xyz/openbmc_project/dump/faultlog',
+        description : 'The fault log dump manager D-Bus object path'
+      )
+
+option('FAULTLOG_DUMP_OBJ_ENTRY', type : 'string',
+        value : '/xyz/openbmc_project/dump/faultlog/entry',
+        description : 'The fault log dump entry D-Bus object path'
+      )
+
 # Configurations for openpower-dump extension
 
 # System dump options