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