Initial commit for the Dump core file monitor infrastructure.
Add an inotify watch to the known core dump location.
Resolves openbmc/openbmc#1504
Change-Id: I0093c9f601d82917ca2efb53a4d47ed98f0eaa7f
Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
diff --git a/dump_watch.hpp b/dump_watch.hpp
new file mode 100644
index 0000000..3f7bceb
--- /dev/null
+++ b/dump_watch.hpp
@@ -0,0 +1,95 @@
+#pragma once
+
+#include <map>
+#include <memory>
+#include <systemd/sd-event.h>
+#include <unistd.h>
+
+namespace phosphor
+{
+namespace dump
+{
+
+/** @struct CustomFd
+ *
+ * RAII wrapper for file descriptor.
+ */
+struct CustomFd
+{
+ private:
+ /** @brief File descriptor */
+ int fd = -1;
+
+ public:
+ CustomFd(const CustomFd&) = delete;
+ CustomFd& operator=(const CustomFd&) = delete;
+ CustomFd(CustomFd&&) = delete;
+ CustomFd& operator=(CustomFd&&) = delete;
+
+ /** @brief Saves File descriptor and uses it to do file operation
+ *
+ * @param[in] fd - File descriptor
+ */
+ CustomFd(int fd) : fd(fd) {}
+
+ ~CustomFd();
+
+ int operator()() const
+ {
+ return fd;
+ }
+};
+
+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