Update core file monitor infrastructre using common inotify

Resolves openbmc/openbmc#1510

Change-Id: I5f73c4330df8a5deab29e29201e8521740e6b047
Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
diff --git a/Makefile.am b/Makefile.am
index 85149c4..638cb0f 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -3,14 +3,13 @@
 # Build these headers, don't install them
 noinst_HEADERS = \
 	dump_entry.hpp \
-	dump_watch.hpp \
+	core_manager.hpp \
 	dump_internal.hpp \
 	dump_manager.hpp \
 	dump_utils.hpp \
 	watch.hpp
 
 nobase_nodist_include_HEADERS = \
-	xyz/openbmc_project/Dump/Monitor/error.hpp \
 	xyz/openbmc_project/Dump/Internal/Create/server.hpp
 
 sbin_PROGRAMS = \
@@ -25,9 +24,9 @@
 	xyz/openbmc_project/Dump/Internal/Create/server.cpp
 
 phosphor_dump_monitor_SOURCES = \
-	dump_watch_main.cpp \
-	dump_watch.cpp \
-	xyz/openbmc_project/Dump/Monitor/error.cpp
+	watch.cpp \
+	core_manager.cpp \
+	core_manager_main.cpp
 
 phosphor_dump_manager_CXXFLAGS = \
 	$(PHOSPHOR_DBUS_INTERFACES_CFLAGS) \
@@ -52,9 +51,7 @@
 # Be sure to build needed files before compiling
 BUILT_SOURCES = \
 	xyz/openbmc_project/Dump/Internal/Create/server.cpp \
-	xyz/openbmc_project/Dump/Internal/Create/server.hpp \
-	xyz/openbmc_project/Dump/Monitor/error.cpp \
-	xyz/openbmc_project/Dump/Monitor/error.hpp
+	xyz/openbmc_project/Dump/Internal/Create/server.hpp
 
 CLEANFILES=${BUILT_SOURCES}
 
@@ -70,12 +67,3 @@
 	@mkdir -p `dirname $@`
 	$(SDBUSPLUSPLUS) -r $(srcdir) interface server-header \
 xyz.openbmc_project.Dump.Internal.Create > $@
-
-xyz/openbmc_project/Dump/Monitor/error.hpp: \
-${top_srcdir}/xyz/openbmc_project/Dump/Monitor.errors.yaml
-	@mkdir -p `dirname $@`
-	$(SDBUSPLUSPLUS) -r $(srcdir) error exception-header xyz.openbmc_project.Dump.Monitor > $@
-
-xyz/openbmc_project/Dump/Monitor/error.cpp: ${top_srcdir}/xyz/openbmc_project/Dump/Monitor.errors.yaml
-	@mkdir -p `dirname $@`
-	$(SDBUSPLUSPLUS) -r $(srcdir) error exception-cpp xyz.openbmc_project.Dump.Monitor > $@
diff --git a/core_manager.cpp b/core_manager.cpp
new file mode 100644
index 0000000..8a91a27
--- /dev/null
+++ b/core_manager.cpp
@@ -0,0 +1,30 @@
+#include <unistd.h>
+#include <sys/inotify.h>
+
+#include "core_manager.hpp"
+
+namespace phosphor
+{
+namespace dump
+{
+namespace core
+{
+namespace manager
+{
+
+void watchCallback(const UserMap& fileInfo)
+{
+    for (const auto& i : fileInfo)
+    {
+        // For any new dump file create dump entry object.
+        if (IN_CLOSE_WRITE == i.second)
+        {
+            //TODO openbmc/openbmc#1795 Enable Dump collection function here
+        }
+    }
+}
+
+} // namespace manager
+} // namespace core
+} // namespace dump
+} // namespace phosphor
diff --git a/core_manager.hpp b/core_manager.hpp
new file mode 100644
index 0000000..19658a5
--- /dev/null
+++ b/core_manager.hpp
@@ -0,0 +1,27 @@
+#pragma once
+
+#include <map>
+
+#include "dump_utils.hpp"
+#include "watch.hpp"
+
+namespace phosphor
+{
+namespace dump
+{
+namespace core
+{
+namespace manager
+{
+
+using UserMap = phosphor::dump::inotify::UserMap;
+
+/** @brief Implementation of core watch call back
+  * @param [in] fileInfo - map of file info  path:event
+  */
+void watchCallback(const UserMap& fileInfo);
+
+} // namespace manager
+} // namepsace core
+} // namespace dump
+} // namespace phosphor
diff --git a/core_manager_main.cpp b/core_manager_main.cpp
new file mode 100644
index 0000000..6f7383f
--- /dev/null
+++ b/core_manager_main.cpp
@@ -0,0 +1,56 @@
+#include <sdbusplus/bus.hpp>
+#include <phosphor-logging/elog-errors.hpp>
+
+#include "xyz/openbmc_project/Common/error.hpp"
+#include "config.h"
+#include "core_manager.hpp"
+#include "watch.hpp"
+
+int main(int argc, char* argv[])
+{
+    using namespace phosphor::logging;
+    using InternalFailure =
+        sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
+
+    auto bus = sdbusplus::bus::new_default();
+    sd_event* event = nullptr;
+    auto rc = sd_event_default(&event);
+    if (rc < 0)
+    {
+        log<level::ERR>("Error occurred during the sd_event_default",
+                        entry("rc=%d", rc));
+        report<InternalFailure>();
+        return -1;
+    }
+    phosphor::dump::EventPtr eventP{event};
+    event = nullptr;
+
+    try
+    {
+        phosphor::dump::inotify::Watch watch(
+            eventP,
+            IN_NONBLOCK,
+            IN_CLOSE_WRITE,
+            EPOLLIN,
+            CORE_FILE_DIR,
+            std::bind(
+                &phosphor::dump::core::manager::watchCallback,
+                std::placeholders::_1));
+
+        auto rc = sd_event_loop(eventP.get());
+        if (rc < 0)
+        {
+            log<level::ERR>("Error occurred during the sd_event_loop",
+                            entry("rc=%d", rc));
+            elog<InternalFailure>();
+        }
+    }
+
+    catch (InternalFailure& e)
+    {
+        commit<InternalFailure>();
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/dump_utils.hpp b/dump_utils.hpp
index 6ebee33..e985112 100644
--- a/dump_utils.hpp
+++ b/dump_utils.hpp
@@ -2,6 +2,7 @@
 
 #include <memory>
 #include <unistd.h>
+#include <systemd/sd-event.h>
 
 namespace phosphor
 {
diff --git a/dump_watch.cpp b/dump_watch.cpp
deleted file mode 100644
index 12a6c73..0000000
--- a/dump_watch.cpp
+++ /dev/null
@@ -1,143 +0,0 @@
-#include <sys/inotify.h>
-#include <experimental/filesystem>
-#include <phosphor-logging/elog-errors.hpp>
-#include <xyz/openbmc_project/Dump/Monitor/error.hpp>
-
-#include "config.h"
-#include "dump_watch.hpp"
-#include "elog-errors.hpp"
-#include "xyz/openbmc_project/Common/error.hpp"
-
-namespace phosphor
-{
-namespace dump
-{
-namespace inotify
-{
-
-using namespace std::string_literals;
-using namespace phosphor::logging;
-using namespace sdbusplus::xyz::openbmc_project::Dump::Monitor::Error;
-using namespace sdbusplus::xyz::openbmc_project::Common::Error;
-namespace fs = std::experimental::filesystem;
-
-Watch::~Watch()
-{
-    if ((fd() >= 0) && (wd >= 0))
-    {
-        inotify_rm_watch(fd(), wd);
-    }
-}
-
-Watch::Watch(sd_event* loop):
-    fd(inotifyInit())
-{
-    // Check if CORE DIR exists.
-    fs::path coreDirPath(CORE_FILE_DIR);
-    if (!fs::is_directory(coreDirPath))
-    {
-        namespace metadata = xyz::openbmc_project::Dump::Monitor;
-        elog<InvalidCorePath>(metadata::InvalidCorePath::PATH(CORE_FILE_DIR));
-    }
-
-    //Check for existing coredumps, Dump manager should handle this before
-    //starting the core monitoring.
-    //This is to handle coredumps created prior to Dump applications start,
-    //or missing coredump reporting due to Dump application crashs.
-    if (!fs::is_empty(coreDirPath))
-    {
-        //TODO openbmc/openbmc#1510 Enable Dump collection function here.
-    }
-
-    wd = inotify_add_watch(fd(), CORE_FILE_DIR, IN_CLOSE_WRITE);
-    if (-1 == wd)
-    {
-        auto error = errno;
-        log<level::ERR>("Error occurred during the inotify_add_watch call",
-                        entry("ERRNO=%s", strerror(error)));
-        elog<InternalFailure>();
-    }
-
-    auto rc = sd_event_add_io(loop,
-                              nullptr,
-                              fd(),
-                              EPOLLIN,
-                              callback,
-                              this);
-    if (0 > rc)
-    {
-        // Failed to add to event loop
-        auto error = errno;
-        log<level::ERR>("Error occurred during the sd_event_add_io call",
-                        entry("ERRNO=%s", strerror(error)));
-        elog<InternalFailure>();
-    }
-}
-
-int Watch::inotifyInit()
-{
-    auto fd = inotify_init1(IN_NONBLOCK);
-
-    if (-1 == fd)
-    {
-        auto error = errno;
-        log<level::ERR>("Error occurred during the inotify_init1",
-                        entry("ERRNO=%s", strerror(error)));
-        elog<InternalFailure>();
-    }
-
-    return fd;
-}
-
-int Watch::callback(sd_event_source* s,
-                    int fd,
-                    uint32_t revents,
-                    void* userdata)
-{
-    if (!(revents & EPOLLIN))
-    {
-        return 0;
-    }
-
-    //Maximum inotify events supported in the buffer
-    constexpr auto maxBytes = sizeof(struct inotify_event) + NAME_MAX + 1;
-    uint8_t buffer[maxBytes];
-
-    auto bytes = read(fd, buffer, maxBytes);
-    if (0 > bytes)
-    {
-        //Failed to read inotify event
-        //Report error and return
-        auto error = errno;
-        log<level::ERR>("Error occurred during the read",
-                        entry("ERRNO=%s", strerror(error)));
-        report<InternalFailure>();
-        return 0;
-    }
-
-    auto offset = 0;
-
-    std::vector<fs::path> corePaths;
-
-    while (offset < bytes)
-    {
-        auto event = reinterpret_cast<inotify_event*>(&buffer[offset]);
-        if ((event->mask & IN_CLOSE_WRITE) && !(event->mask & IN_ISDIR))
-        {
-            corePaths.emplace_back(fs::path(CORE_FILE_DIR) / event->name);
-        }
-
-        offset += offsetof(inotify_event, name) + event->len;
-    }
-
-    // Generate new BMC Dump( Core dump Type) incase valid cores
-    if (!corePaths.empty())
-    {
-        //TODO openbmc/openbmc#1510 Enable Dump collection function here
-    }
-    return 0;
-}
-
-} // namespace inotify
-} // namespace dump
-} // namespace phosphor
diff --git a/dump_watch.hpp b/dump_watch.hpp
deleted file mode 100644
index fdd301c..0000000
--- a/dump_watch.hpp
+++ /dev/null
@@ -1,64 +0,0 @@
-#pragma once
-
-#include <systemd/sd-event.h>
-#include <unistd.h>
-#include "dump_utils.hpp"
-
-namespace phosphor
-{
-namespace dump
-{
-namespace inotify
-{
-
-/** @class Watch
- *
- *  @brief Adds inotify watch on core file directories.
- *
- *  The inotify watch is hooked up with sd-event, so that on call back,
- *  appropriate actions are taken to collect the core files.
- */
-class Watch
-{
-    public:
-        /** @brief ctor - hook inotify watch with sd-event
-         *
-         *  @param[in] loop - sd-event object
-         */
-        Watch(sd_event* loop);
-
-        Watch(const Watch&) = delete;
-        Watch& operator=(const Watch&) = delete;
-        Watch(Watch&&) = default;
-        Watch& operator=(Watch&&) = default;
-
-        /* @brief dtor - remove inotify watch and close fd's */
-        ~Watch();
-
-    private:
-        /** @brief sd-event callback
-         *
-         *  @param[in] s - event source, floating (unused) in our case
-         *  @param[in] fd - inotify fd
-         *  @param[in] revents - events that matched for fd
-         *  @param[in] userdata - pointer to Watch object
-         *  @returns 0 on success, -1 on fail
-         */
-        static int callback(sd_event_source* s,
-                            int fd,
-                            uint32_t revents,
-                            void* userdata);
-
-        /**  initialize an inotify instance and returns file descriptor */
-        int inotifyInit();
-
-        /** @brief core file directory watch descriptor */
-        int wd = -1;
-
-        /** @brief file descriptor manager */
-        CustomFd fd;
-};
-
-} // namespace inotify
-} // namespace dump
-} // namespace phosphor
diff --git a/dump_watch_main.cpp b/dump_watch_main.cpp
deleted file mode 100644
index d061ae1..0000000
--- a/dump_watch_main.cpp
+++ /dev/null
@@ -1,43 +0,0 @@
-#include "dump_watch.hpp"
-#include "elog-errors.hpp"
-#include "xyz/openbmc_project/Dump/Monitor/error.hpp"
-#include "xyz/openbmc_project/Common/error.hpp"
-
-#include <phosphor-logging/log.hpp>
-#include <phosphor-logging/elog.hpp>
-#include <phosphor-logging/elog-errors.hpp>
-
-int main(int argc, char* argv[])
-{
-    sd_event* loop = nullptr;
-    sd_event_default(&loop);
-
-    using namespace phosphor::logging;
-
-    using InvalidCorePath =
-        sdbusplus::xyz::openbmc_project::Dump::Monitor::Error::InvalidCorePath;
-    using InternalFailure =
-        sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
-
-    try
-    {
-        phosphor::dump::inotify::Watch watch(loop);
-        sd_event_loop(loop);
-    }
-
-    catch (InvalidCorePath& e)
-    {
-        commit<InvalidCorePath>();
-        return -1;
-    }
-
-    catch (InternalFailure& e)
-    {
-        commit<InternalFailure>();
-        return -1;
-    }
-
-    sd_event_unref(loop);
-
-    return 0;
-}
diff --git a/elog-errors.hpp b/elog-errors.hpp
deleted file mode 100644
index f5c6466..0000000
--- a/elog-errors.hpp
+++ /dev/null
@@ -1,89 +0,0 @@
-// This file was autogenerated.  Do not edit!
-// See elog-gen.py for more details
-#pragma once
-
-#include <string>
-#include <tuple>
-#include <type_traits>
-#include <sdbusplus/exception.hpp>
-#include <phosphor-logging/log.hpp>
-#include <phosphor-logging/elog.hpp>
-
-namespace sdbusplus
-{
-namespace xyz
-{
-namespace openbmc_project
-{
-namespace Dump
-{
-namespace Monitor
-{
-namespace Error
-{
-    struct InvalidCorePath;
-} // namespace Error
-} // namespace Monitor
-} // namespace Dump
-} // namespace openbmc_project
-} // namespace xyz
-} // namespace sdbusplus
-
-
-namespace phosphor
-{
-
-namespace logging
-{
-
-namespace xyz
-{
-namespace openbmc_project
-{
-namespace Dump
-{
-namespace Monitor
-{
-namespace _InvalidCorePath
-{
-
-struct PATH
-{
-    static constexpr auto str = "PATH=%s";
-    static constexpr auto str_short = "PATH";
-    using type = std::tuple<std::decay_t<decltype(str)>,const char*>;
-    explicit constexpr PATH(const char* a) : _entry(entry(str, a)) {};
-    type _entry;
-};
-
-}  // namespace _InvalidCorePath
-
-struct InvalidCorePath
-{
-    static constexpr auto L = level::ERR;
-    using PATH = _InvalidCorePath::PATH;
-    using metadata_types = std::tuple<PATH>;
-
-};
-
-} // namespace Monitor
-} // namespace Dump
-} // namespace openbmc_project
-} // namespace xyz
-
-
-namespace details
-{
-
-template <>
-struct map_exception_type<sdbusplus::xyz::openbmc_project::Dump::Monitor::Error::InvalidCorePath>
-{
-    using type = xyz::openbmc_project::Dump::Monitor::InvalidCorePath;
-};
-
-}
-
-
-} // namespace logging
-
-} // namespace phosphor
diff --git a/xyz/openbmc_project/Dump/Monitor.errors.yaml b/xyz/openbmc_project/Dump/Monitor.errors.yaml
deleted file mode 100644
index 7a2478c..0000000
--- a/xyz/openbmc_project/Dump/Monitor.errors.yaml
+++ /dev/null
@@ -1,2 +0,0 @@
-- name: InvalidCorePath
-  description: Invalid core directory path
diff --git a/xyz/openbmc_project/Dump/Monitor.metadata.yaml b/xyz/openbmc_project/Dump/Monitor.metadata.yaml
deleted file mode 100644
index 045a4ea..0000000
--- a/xyz/openbmc_project/Dump/Monitor.metadata.yaml
+++ /dev/null
@@ -1,5 +0,0 @@
-- name: InvalidCorePath
-  level: ERR
-  meta:
-    - str: "PATH=%s"
-      type: string