Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 3 | #include "dump_utils.hpp" |
| 4 | |
| 5 | #include <sys/inotify.h> |
| 6 | #include <systemd/sd-event.h> |
| 7 | |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 8 | #include <filesystem> |
Brad Bishop | 7861079 | 2018-02-21 13:08:15 -0500 | [diff] [blame] | 9 | #include <functional> |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 10 | #include <map> |
| 11 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace dump |
| 15 | { |
| 16 | namespace inotify |
| 17 | { |
| 18 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 19 | // User specific call back function input map(path:event) type. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 20 | using UserMap = std::map<std::filesystem::path, uint32_t>; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 21 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 22 | // User specific callback function wrapper type. |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 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 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 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, |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 47 | const std::filesystem::path& path, UserType userFunc); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 48 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 49 | Watch(const Watch&) = delete; |
| 50 | Watch& operator=(const Watch&) = delete; |
| 51 | Watch(Watch&&) = default; |
| 52 | Watch& operator=(Watch&&) = default; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 53 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 54 | /* @brief dtor - remove inotify watch and close fd's */ |
| 55 | ~Watch(); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 56 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 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); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 71 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 72 | /** initialize an inotify instance and returns file descriptor */ |
| 73 | int inotifyInit(); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 74 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 75 | /** @brief inotify flags */ |
| 76 | int flags; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 77 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 78 | /** @brief Mask of events */ |
| 79 | uint32_t mask; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 80 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 81 | /** @brief Events to be watched */ |
| 82 | uint32_t events; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 83 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 84 | /** @brief File path to be watched */ |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 85 | std::filesystem::path path; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 86 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 87 | /** @brief dump file directory watch descriptor */ |
| 88 | int wd = -1; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 89 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 90 | /** @brief file descriptor manager */ |
| 91 | CustomFd fd; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 92 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 93 | /** @brief The user level callback function wrapper */ |
| 94 | UserType userFunc; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | } // namespace inotify |
| 98 | } // namespace dump |
| 99 | } // namespace phosphor |