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