Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <systemd/sd-event.h> |
| 4 | #include <unistd.h> |
| 5 | |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 6 | #include <functional> |
| 7 | #include <memory> |
| 8 | |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 9 | namespace openpower |
| 10 | { |
| 11 | namespace software |
| 12 | { |
| 13 | namespace updater |
| 14 | { |
| 15 | |
| 16 | /* Need a custom deleter for freeing up sd_event_source */ |
| 17 | struct EventSourceDeleter |
| 18 | { |
| 19 | void operator()(sd_event_source* eventSource) const |
| 20 | { |
| 21 | eventSource = sd_event_source_unref(eventSource); |
| 22 | } |
| 23 | }; |
| 24 | using EventSourcePtr = std::unique_ptr<sd_event_source, EventSourceDeleter>; |
| 25 | |
| 26 | /** @struct CustomFd |
| 27 | * |
| 28 | * RAII wrapper for file descriptor. |
| 29 | */ |
| 30 | struct CustomFd |
| 31 | { |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 32 | public: |
| 33 | CustomFd() = delete; |
| 34 | CustomFd(const CustomFd&) = delete; |
| 35 | CustomFd& operator=(const CustomFd&) = delete; |
| 36 | CustomFd(CustomFd&&) = delete; |
| 37 | CustomFd& operator=(CustomFd&&) = delete; |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 38 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 39 | /** @brief Saves File descriptor and uses it to do file operation |
| 40 | * |
| 41 | * @param[in] fd - File descriptor |
| 42 | */ |
| 43 | CustomFd(int fd) : fd(fd) |
| 44 | { |
| 45 | } |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 46 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 47 | ~CustomFd() |
| 48 | { |
| 49 | if (fd >= 0) |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 50 | { |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 51 | close(fd); |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 52 | } |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 53 | } |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 54 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 55 | int operator()() const |
| 56 | { |
| 57 | return fd; |
| 58 | } |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 59 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 60 | private: |
| 61 | /** @brief File descriptor */ |
| 62 | int fd = -1; |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | /** @class Watch |
| 66 | * |
| 67 | * @brief Adds inotify watch on PNOR symlinks file to monitor for changes in |
| 68 | * "running" PNOR version |
| 69 | * |
| 70 | * The inotify watch is hooked up with sd-event, so that on call back, |
| 71 | * appropriate actions related to a change in the "running" PNOR version |
| 72 | * can be taken. |
| 73 | */ |
| 74 | class Watch |
| 75 | { |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 76 | public: |
| 77 | /** @brief ctor - hook inotify watch with sd-event |
| 78 | * |
| 79 | * @param[in] loop - sd-event object |
| 80 | * @param[in] functionalCallback - The callback function for updating |
| 81 | * the functional associations. |
| 82 | */ |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame] | 83 | Watch(sd_event* loop, |
| 84 | std::function<void(const std::string&)> functionalCallback); |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 85 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 86 | Watch(const Watch&) = delete; |
| 87 | Watch& operator=(const Watch&) = delete; |
| 88 | Watch(Watch&&) = delete; |
| 89 | Watch& operator=(Watch&&) = delete; |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 90 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 91 | /** @brief dtor - remove inotify watch |
| 92 | */ |
| 93 | ~Watch(); |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 94 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 95 | private: |
| 96 | /** @brief sd-event callback |
| 97 | * |
| 98 | * @param[in] s - event source, floating (unused) in our case |
| 99 | * @param[in] fd - inotify fd |
| 100 | * @param[in] revents - events that matched for fd |
| 101 | * @param[in] userdata - pointer to Watch object |
| 102 | * @returns 0 on success, -1 on fail |
| 103 | */ |
| 104 | static int callback(sd_event_source* s, int fd, uint32_t revents, |
| 105 | void* userdata); |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 106 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 107 | /** initialize an inotify instance and returns file descriptor */ |
| 108 | int inotifyInit(); |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 109 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 110 | /** @brief PNOR symlink file watch descriptor */ |
| 111 | int wd = -1; |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 112 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 113 | /** @brief event source */ |
| 114 | EventSourcePtr eventSource; |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 115 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 116 | /** @brief The callback function for updating the |
| 117 | functional associations. */ |
| 118 | std::function<void(std::string&)> functionalCallback; |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 119 | |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 120 | /** @brief inotify file descriptor */ |
| 121 | CustomFd fd; |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | } // namespace updater |
| 125 | } // namespace software |
| 126 | } // namespace openpower |