1671fc7f3SJayanth Othayoth #pragma once 2671fc7f3SJayanth Othayoth 3cb65ffceSJayanth Othayoth #include "dump_utils.hpp" 4cb65ffceSJayanth Othayoth 5cb65ffceSJayanth Othayoth #include <sys/inotify.h> 6cb65ffceSJayanth Othayoth #include <systemd/sd-event.h> 7cb65ffceSJayanth Othayoth 83fc6df48SJayanth Othayoth #include <filesystem> 97861079eSBrad Bishop #include <functional> 10671fc7f3SJayanth Othayoth #include <map> 11671fc7f3SJayanth Othayoth 12671fc7f3SJayanth Othayoth namespace phosphor 13671fc7f3SJayanth Othayoth { 14671fc7f3SJayanth Othayoth namespace dump 15671fc7f3SJayanth Othayoth { 16671fc7f3SJayanth Othayoth namespace inotify 17671fc7f3SJayanth Othayoth { 18671fc7f3SJayanth Othayoth 1995a72983SGunnar Mills // User specific call back function input map(path:event) type. 203fc6df48SJayanth Othayoth using UserMap = std::map<std::filesystem::path, uint32_t>; 21671fc7f3SJayanth Othayoth 22671fc7f3SJayanth Othayoth // User specific callback function wrapper type. 23671fc7f3SJayanth Othayoth using UserType = std::function<void(const UserMap&)>; 24671fc7f3SJayanth Othayoth 25671fc7f3SJayanth Othayoth /** @class Watch 26671fc7f3SJayanth Othayoth * 27671fc7f3SJayanth Othayoth * @brief Adds inotify watch on directory. 28671fc7f3SJayanth Othayoth * 29671fc7f3SJayanth Othayoth * The inotify watch is hooked up with sd-event, so that on call back, 30671fc7f3SJayanth Othayoth * appropriate actions are taken to collect files from the directory 31671fc7f3SJayanth Othayoth * initialized by the object. 32671fc7f3SJayanth Othayoth */ 33671fc7f3SJayanth Othayoth class Watch 34671fc7f3SJayanth Othayoth { 35671fc7f3SJayanth Othayoth public: 36671fc7f3SJayanth Othayoth /** @brief ctor - hook inotify watch with sd-event 37671fc7f3SJayanth Othayoth * 38671fc7f3SJayanth Othayoth * @param[in] eventObj - Event loop object 39671fc7f3SJayanth Othayoth * @param[in] flags - inotify flags 40671fc7f3SJayanth Othayoth * @param[in] mask - Mask of events 41671fc7f3SJayanth Othayoth * @param[in] events - Events to be watched 42671fc7f3SJayanth Othayoth * @param[in] path - File path to be watched 43671fc7f3SJayanth Othayoth * @param[in] userFunc - User specific callback fnction wrapper. 44671fc7f3SJayanth Othayoth * 45671fc7f3SJayanth Othayoth */ 46cb65ffceSJayanth Othayoth Watch(const EventPtr& eventObj, int flags, uint32_t mask, uint32_t events, 473fc6df48SJayanth Othayoth const std::filesystem::path& path, UserType userFunc); 48671fc7f3SJayanth Othayoth 49671fc7f3SJayanth Othayoth Watch(const Watch&) = delete; 50671fc7f3SJayanth Othayoth Watch& operator=(const Watch&) = delete; 514f68fc46SJayanth Othayoth Watch(Watch&&) = delete; 524f68fc46SJayanth Othayoth Watch& operator=(Watch&&) = delete; 53671fc7f3SJayanth Othayoth 54671fc7f3SJayanth Othayoth /* @brief dtor - remove inotify watch and close fd's */ 55671fc7f3SJayanth Othayoth ~Watch(); 56671fc7f3SJayanth Othayoth 57671fc7f3SJayanth Othayoth private: 58671fc7f3SJayanth Othayoth /** @brief sd-event callback. 59671fc7f3SJayanth Othayoth * @details Collects the files and event info and call the 60671fc7f3SJayanth Othayoth * appropriate user function for further action. 61671fc7f3SJayanth Othayoth * 62671fc7f3SJayanth Othayoth * @param[in] s - event source, floating (unused) in our case 63671fc7f3SJayanth Othayoth * @param[in] fd - inotify fd 64671fc7f3SJayanth Othayoth * @param[in] revents - events that matched for fd 65671fc7f3SJayanth Othayoth * @param[in] userdata - pointer to Watch object 66671fc7f3SJayanth Othayoth * 67671fc7f3SJayanth Othayoth * @returns 0 on success, -1 on fail 68671fc7f3SJayanth Othayoth */ 69cb65ffceSJayanth Othayoth static int callback(sd_event_source* s, int fd, uint32_t revents, 70671fc7f3SJayanth Othayoth void* userdata); 71671fc7f3SJayanth Othayoth 72671fc7f3SJayanth Othayoth /** initialize an inotify instance and returns file descriptor */ 73671fc7f3SJayanth Othayoth int inotifyInit(); 74671fc7f3SJayanth Othayoth 75671fc7f3SJayanth Othayoth /** @brief inotify flags */ 76671fc7f3SJayanth Othayoth int flags; 77671fc7f3SJayanth Othayoth 78671fc7f3SJayanth Othayoth /** @brief Mask of events */ 79671fc7f3SJayanth Othayoth uint32_t mask; 80671fc7f3SJayanth Othayoth 81671fc7f3SJayanth Othayoth /** @brief Events to be watched */ 82671fc7f3SJayanth Othayoth uint32_t events; 83671fc7f3SJayanth Othayoth 84671fc7f3SJayanth Othayoth /** @brief File path to be watched */ 853fc6df48SJayanth Othayoth std::filesystem::path path; 86671fc7f3SJayanth Othayoth 87671fc7f3SJayanth Othayoth /** @brief dump file directory watch descriptor */ 88671fc7f3SJayanth Othayoth int wd = -1; 89671fc7f3SJayanth Othayoth 90671fc7f3SJayanth Othayoth /** @brief file descriptor manager */ 91671fc7f3SJayanth Othayoth CustomFd fd; 92671fc7f3SJayanth Othayoth 93671fc7f3SJayanth Othayoth /** @brief The user level callback function wrapper */ 94671fc7f3SJayanth Othayoth UserType userFunc; 95*fa6a47bfSJian Zhang 96*fa6a47bfSJian Zhang /** @brief The event source object reference */ 97*fa6a47bfSJian Zhang sd_event_source* source = nullptr; 98671fc7f3SJayanth Othayoth }; 99671fc7f3SJayanth Othayoth 100671fc7f3SJayanth Othayoth } // namespace inotify 101671fc7f3SJayanth Othayoth } // namespace dump 102671fc7f3SJayanth Othayoth } // namespace phosphor 103