1 #pragma once 2 3 #include "dump_utils.hpp" 4 5 #include <sys/inotify.h> 6 #include <systemd/sd-event.h> 7 8 #include <filesystem> 9 #include <functional> 10 #include <map> 11 12 namespace phosphor 13 { 14 namespace dump 15 { 16 namespace inotify 17 { 18 19 // User specific call back function input map(path:event) type. 20 using UserMap = std::map<std::filesystem::path, uint32_t>; 21 22 // User specific callback function wrapper type. 23 using UserType = std::function<void(const UserMap&)>; 24 25 /** @class Watch 26 * 27 * @brief Adds inotify watch on directory. 28 * 29 * The inotify watch is hooked up with sd-event, so that on call back, 30 * appropriate actions are taken to collect files from the directory 31 * initialized by the object. 32 */ 33 class Watch 34 { 35 public: 36 /** @brief ctor - hook inotify watch with sd-event 37 * 38 * @param[in] eventObj - Event loop object 39 * @param[in] flags - inotify flags 40 * @param[in] mask - Mask of events 41 * @param[in] events - Events to be watched 42 * @param[in] path - File path to be watched 43 * @param[in] userFunc - User specific callback fnction wrapper. 44 * 45 */ 46 Watch(const EventPtr& eventObj, int flags, uint32_t mask, uint32_t events, 47 const std::filesystem::path& path, UserType userFunc); 48 49 Watch(const Watch&) = delete; 50 Watch& operator=(const Watch&) = delete; 51 Watch(Watch&&) = default; 52 Watch& operator=(Watch&&) = default; 53 54 /* @brief dtor - remove inotify watch and close fd's */ 55 ~Watch(); 56 57 private: 58 /** @brief sd-event callback. 59 * @details Collects the files and event info and call the 60 * appropriate user function for further action. 61 * 62 * @param[in] s - event source, floating (unused) in our case 63 * @param[in] fd - inotify fd 64 * @param[in] revents - events that matched for fd 65 * @param[in] userdata - pointer to Watch object 66 * 67 * @returns 0 on success, -1 on fail 68 */ 69 static int callback(sd_event_source* s, int fd, uint32_t revents, 70 void* userdata); 71 72 /** initialize an inotify instance and returns file descriptor */ 73 int inotifyInit(); 74 75 /** @brief inotify flags */ 76 int flags; 77 78 /** @brief Mask of events */ 79 uint32_t mask; 80 81 /** @brief Events to be watched */ 82 uint32_t events; 83 84 /** @brief File path to be watched */ 85 std::filesystem::path path; 86 87 /** @brief dump file directory watch descriptor */ 88 int wd = -1; 89 90 /** @brief file descriptor manager */ 91 CustomFd fd; 92 93 /** @brief The user level callback function wrapper */ 94 UserType userFunc; 95 }; 96 97 } // namespace inotify 98 } // namespace dump 99 } // namespace phosphor 100