Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "util.hpp" |
| 4 | #include "types.hpp" |
| 5 | #include "dns_updater.hpp" |
| 6 | |
| 7 | #include <systemd/sd-event.h> |
| 8 | |
| 9 | #include <sys/inotify.h> |
| 10 | #include <map> |
| 11 | #include <functional> |
| 12 | #include <experimental/filesystem> |
| 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
| 18 | namespace inotify |
| 19 | { |
| 20 | |
| 21 | namespace fs = std::experimental::filesystem; |
| 22 | |
| 23 | // Auxiliary callback to be invoked on inotify events |
| 24 | using UserCallBack = std::function<void(const std::string&)>; |
| 25 | |
| 26 | /** @class Watch |
| 27 | * |
| 28 | * @brief Adds inotify watch on directory |
| 29 | * |
| 30 | * @details Calls back user function on matching events |
| 31 | */ |
| 32 | class Watch |
| 33 | { |
| 34 | public: |
| 35 | Watch() = delete; |
| 36 | Watch(const Watch&) = delete; |
| 37 | Watch& operator=(const Watch&) = delete; |
| 38 | Watch(Watch&&) = delete; |
| 39 | Watch& operator=(Watch&&) = delete; |
| 40 | |
| 41 | /** @brief Hooks inotify watch with sd-event |
| 42 | * |
| 43 | * @param[in] eventPtr - Reference to sd_event wrapped in unique_ptr |
| 44 | * @param[in] path - File path to be watched |
| 45 | * @param[in] userFunc - User specific callback function on events |
| 46 | * @param[in] flags - Flags to be supplied to inotify |
| 47 | * @param[in] mask - Mask of events to be supplied to inotify |
| 48 | * @param[in] events - Events to be watched |
| 49 | */ |
| 50 | Watch(phosphor::network::EventPtr& eventPtr, |
| 51 | const fs::path path, |
| 52 | UserCallBack userFunc, |
| 53 | int flags = IN_NONBLOCK, |
| 54 | uint32_t mask = IN_CLOSE_WRITE, |
| 55 | uint32_t events = EPOLLIN); |
| 56 | |
| 57 | /** @brief Remove inotify watch and close fd's */ |
| 58 | ~Watch() |
| 59 | { |
| 60 | if ((fd() >= 0) && (wd >= 0)) |
| 61 | { |
| 62 | inotify_rm_watch(fd(), wd); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | /** @brief Callback invoked when inotify event fires |
| 68 | * |
| 69 | * @details On a matching event, calls back into user supplied |
| 70 | * function if there is one registered |
| 71 | * |
| 72 | * @param[in] eventSource - Event source |
| 73 | * @param[in] fd - Inotify fd |
| 74 | * @param[in] retEvents - Events that matched for fd |
| 75 | * @param[in] userData - Pointer to Watch object |
| 76 | * |
| 77 | * @returns 0 on success, -1 on fail |
| 78 | */ |
| 79 | static int processEvents(sd_event_source* eventSource, |
| 80 | int fd, |
| 81 | uint32_t retEvents, |
| 82 | void* userData); |
| 83 | |
| 84 | /** @brief Initializes an inotify instance |
| 85 | * |
| 86 | * @return Descriptor on success, -1 on failure |
| 87 | */ |
| 88 | int inotifyInit(); |
| 89 | |
| 90 | /** @brief File path to be watched */ |
| 91 | const fs::path path; |
| 92 | |
| 93 | /** @brief User callback function */ |
| 94 | UserCallBack userFunc; |
| 95 | |
| 96 | /** @brief Inotify flags */ |
| 97 | int flags; |
| 98 | |
| 99 | /** @brief Mask of events */ |
| 100 | uint32_t mask; |
| 101 | |
| 102 | /** @brief Events to be watched */ |
| 103 | uint32_t events; |
| 104 | |
| 105 | /** @brief Watch descriptor */ |
| 106 | int wd = -1; |
| 107 | |
| 108 | /** @brief File descriptor manager */ |
| 109 | phosphor::Descriptor fd; |
| 110 | }; |
| 111 | |
| 112 | } // namespace inotify |
| 113 | } // namespace network |
| 114 | } // namespace phosphor |