PEL: Watch for manually deleted PEL files

Currently, if someone were to manually delete a PEL file, the Repository
class would not know to remove it from its _pelAttributes index which
will lead to problems down the line.

To fix this, create an inotify watch that looks for files to be deleted
in the PEL directory.  When one or more is deleted, remove it from the
Repository class and also remove the OpenBMC event log for it.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I238c0b886b01e88cc931162a0596b848d1d975b1
diff --git a/extensions/openpower-pels/manager.hpp b/extensions/openpower-pels/manager.hpp
index 2d443c6..50317a5 100644
--- a/extensions/openpower-pels/manager.hpp
+++ b/extensions/openpower-pels/manager.hpp
@@ -31,7 +31,6 @@
 {
   public:
     Manager() = delete;
-    ~Manager() = default;
     Manager(const Manager&) = default;
     Manager& operator=(const Manager&) = default;
     Manager(Manager&&) = default;
@@ -52,8 +51,10 @@
         _logManager(logManager), _eventLogger(std::move(creatorFunc)),
         _repo(getPELRepoPath()),
         _registry(getPELReadOnlyDataPath() / message::registryFileName),
+        _event(sdeventplus::Event::get_default()),
         _dataIface(std::move(dataIface))
     {
+        setupPELDeleteWatch();
     }
 
     /**
@@ -76,6 +77,11 @@
     }
 
     /**
+     * @brief Destructor
+     */
+    ~Manager();
+
+    /**
      * @brief Creates a PEL based on the OpenBMC event log contents.  If
      *        a PEL was passed in via the RAWPEL specifier in the
      *        additionalData parameter, use that instead.
@@ -271,6 +277,21 @@
     void pruneRepo(sdeventplus::source::EventBase& source);
 
     /**
+     * @brief Sets up an inotify watch to watch for deleted PEL
+     *        files.  Calls pelFileDeleted() when that occurs.
+     */
+    void setupPELDeleteWatch();
+
+    /**
+     * @brief Called when the inotify watch put on the repository directory
+     *        detects a PEL file was deleted.
+     *
+     * Will tell the Repository class about the deleted PEL, and then tell
+     * the log manager class to delete the corresponding OpenBMC event log.
+     */
+    void pelFileDeleted(sdeventplus::source::IO& io, int fd, uint32_t revents);
+
+    /**
      * @brief Reference to phosphor-logging's Manager class
      */
     phosphor::logging::internal::Manager& _logManager;
@@ -292,6 +313,11 @@
     message::Registry _registry;
 
     /**
+     * @brief The Event object this class uses
+     */
+    sdeventplus::Event _event;
+
+    /**
      * @brief The API the PEL sections use to gather data
      */
     std::unique_ptr<DataInterfaceBase> _dataIface;
@@ -313,6 +339,22 @@
      *        running out of space to make room for new ones.
      */
     std::unique_ptr<sdeventplus::source::Defer> _repoPrunerEventSource;
+
+    /**
+     * @brief The even source for watching for deleted PEL files.
+     */
+    std::unique_ptr<sdeventplus::source::IO> _pelFileDeleteEventSource;
+
+    /**
+     * @brief The file descriptor returned by inotify_init1() used
+     *        for watching for deleted PEL files.
+     */
+    int _pelFileDeleteFD = -1;
+
+    /**
+     * @brief The file descriptor returned by inotify_add_watch().
+     */
+    int _pelFileDeleteWatchFD = -1;
 };
 
 } // namespace pels