Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 3 | #include "dns_updater.hpp" |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 4 | #include "types.hpp" |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 5 | #include "util.hpp" |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 6 | |
| 7 | #include <sys/inotify.h> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 8 | #include <systemd/sd-event.h> |
| 9 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 10 | #include <filesystem> |
Patrick Venture | 189d44e | 2018-07-09 12:30:59 -0700 | [diff] [blame] | 11 | #include <functional> |
| 12 | #include <map> |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace network |
| 17 | { |
| 18 | namespace inotify |
| 19 | { |
| 20 | |
Manojkiran Eda | a879baa | 2020-06-13 14:39:08 +0530 | [diff] [blame] | 21 | namespace fs = std::filesystem; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 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 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 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; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 40 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 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, const fs::path path, |
| 51 | UserCallBack userFunc, int flags = IN_NONBLOCK, |
| 52 | uint32_t mask = IN_CLOSE_WRITE, uint32_t events = EPOLLIN); |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 53 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 54 | /** @brief Remove inotify watch and close fd's */ |
| 55 | ~Watch() |
| 56 | { |
| 57 | if ((fd() >= 0) && (wd >= 0)) |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 58 | { |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 59 | inotify_rm_watch(fd(), wd); |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 60 | } |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 61 | } |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 62 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 63 | private: |
| 64 | /** @brief Callback invoked when inotify event fires |
| 65 | * |
| 66 | * @details On a matching event, calls back into user supplied |
| 67 | * function if there is one registered |
| 68 | * |
| 69 | * @param[in] eventSource - Event source |
| 70 | * @param[in] fd - Inotify fd |
| 71 | * @param[in] retEvents - Events that matched for fd |
| 72 | * @param[in] userData - Pointer to Watch object |
| 73 | * |
| 74 | * @returns 0 on success, -1 on fail |
| 75 | */ |
| 76 | static int processEvents(sd_event_source* eventSource, int fd, |
| 77 | uint32_t retEvents, void* userData); |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 78 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 79 | /** @brief Initializes an inotify instance |
| 80 | * |
| 81 | * @return Descriptor on success, -1 on failure |
| 82 | */ |
| 83 | int inotifyInit(); |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 84 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 85 | /** @brief File path to be watched */ |
| 86 | const fs::path path; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 87 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 88 | /** @brief User callback function */ |
| 89 | UserCallBack userFunc; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 90 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 91 | /** @brief Inotify flags */ |
| 92 | int flags; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 93 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 94 | /** @brief Mask of events */ |
| 95 | uint32_t mask; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 96 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 97 | /** @brief Events to be watched */ |
| 98 | uint32_t events; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 99 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 100 | /** @brief Watch descriptor */ |
| 101 | int wd = -1; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 102 | |
Gunnar Mills | 57d9c50 | 2018-09-14 14:42:34 -0500 | [diff] [blame] | 103 | /** @brief File descriptor manager */ |
| 104 | phosphor::Descriptor fd; |
Vishwanatha Subbanna | ca4ce1b | 2017-10-16 23:17:18 +0530 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | } // namespace inotify |
| 108 | } // namespace network |
| 109 | } // namespace phosphor |