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 | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 8 | #include <experimental/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 | |
| 19 | namespace fs = std::experimental::filesystem; |
| 20 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 21 | // User specific call back function input map(path:event) type. |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 22 | using UserMap = std::map<fs::path, uint32_t>; |
| 23 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 24 | // User specific callback function wrapper type. |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 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 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 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); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 50 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 51 | Watch(const Watch&) = delete; |
| 52 | Watch& operator=(const Watch&) = delete; |
| 53 | Watch(Watch&&) = default; |
| 54 | Watch& operator=(Watch&&) = default; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 55 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 56 | /* @brief dtor - remove inotify watch and close fd's */ |
| 57 | ~Watch(); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 58 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 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); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 73 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 74 | /** initialize an inotify instance and returns file descriptor */ |
| 75 | int inotifyInit(); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 76 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 77 | /** @brief inotify flags */ |
| 78 | int flags; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 79 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 80 | /** @brief Mask of events */ |
| 81 | uint32_t mask; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 82 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 83 | /** @brief Events to be watched */ |
| 84 | uint32_t events; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 85 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 86 | /** @brief File path to be watched */ |
| 87 | fs::path path; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 88 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 89 | /** @brief dump file directory watch descriptor */ |
| 90 | int wd = -1; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 91 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 92 | /** @brief file descriptor manager */ |
| 93 | CustomFd fd; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 94 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 95 | /** @brief The user level callback function wrapper */ |
| 96 | UserType userFunc; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | } // namespace inotify |
| 100 | } // namespace dump |
| 101 | } // namespace phosphor |