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