Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "watch.hpp" |
| 4 | |
Lei YU | bee5140 | 2019-02-26 11:36:34 +0800 | [diff] [blame] | 5 | #include "item_updater_ubi.hpp" |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 6 | |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 7 | #include <sys/inotify.h> |
| 8 | #include <unistd.h> |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 9 | |
Brad Bishop | 8facccf | 2020-11-04 09:44:58 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/log.hpp> |
| 11 | |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 12 | #include <cstddef> |
| 13 | #include <cstring> |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 14 | #include <filesystem> |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 15 | #include <functional> |
Gunnar Mills | f6ed589 | 2018-09-07 17:08:02 -0500 | [diff] [blame] | 16 | #include <stdexcept> |
| 17 | #include <string> |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 18 | |
| 19 | namespace openpower |
| 20 | { |
| 21 | namespace software |
| 22 | { |
| 23 | namespace updater |
| 24 | { |
| 25 | |
| 26 | using namespace phosphor::logging; |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 27 | |
| 28 | Watch::Watch(sd_event* loop, |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame] | 29 | std::function<void(const std::string&)> functionalCallback) : |
Patrick Williams | f8e0242 | 2024-08-16 15:19:59 -0400 | [diff] [blame^] | 30 | functionalCallback(functionalCallback), fd(inotifyInit()) |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 31 | |
| 32 | { |
| 33 | // Create PNOR_ACTIVE_PATH if doesn't exist. |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 34 | if (!std::filesystem::is_directory(PNOR_ACTIVE_PATH)) |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 35 | { |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 36 | std::filesystem::create_directories(PNOR_ACTIVE_PATH); |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | wd = inotify_add_watch(fd(), PNOR_ACTIVE_PATH, IN_CREATE); |
| 40 | if (-1 == wd) |
| 41 | { |
| 42 | auto error = errno; |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 43 | throw std::system_error(error, std::generic_category(), |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 44 | "Error occurred during the inotify_init1"); |
| 45 | } |
| 46 | |
| 47 | decltype(eventSource.get()) sourcePtr = nullptr; |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 48 | auto rc = sd_event_add_io(loop, &sourcePtr, fd(), EPOLLIN, callback, this); |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 49 | |
| 50 | eventSource.reset(sourcePtr); |
| 51 | |
| 52 | if (0 > rc) |
| 53 | { |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 54 | throw std::system_error(-rc, std::generic_category(), |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 55 | "Error occurred during the inotify_init1"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | Watch::~Watch() |
| 60 | { |
| 61 | if ((-1 != fd()) && (-1 != wd)) |
| 62 | { |
| 63 | inotify_rm_watch(fd(), wd); |
| 64 | } |
| 65 | } |
| 66 | |
Brad Bishop | c8f2250 | 2020-11-06 14:42:09 -0500 | [diff] [blame] | 67 | int Watch::callback(sd_event_source*, int fd, uint32_t revents, void* userdata) |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 68 | { |
| 69 | if (!(revents & EPOLLIN)) |
| 70 | { |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | constexpr auto maxBytes = 1024; |
| 75 | uint8_t buffer[maxBytes]; |
| 76 | auto bytes = read(fd, buffer, maxBytes); |
| 77 | if (0 > bytes) |
| 78 | { |
| 79 | auto error = errno; |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 80 | throw std::system_error(error, std::generic_category(), |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 81 | "failed to read inotify event"); |
| 82 | } |
| 83 | |
| 84 | auto offset = 0; |
| 85 | while (offset < bytes) |
| 86 | { |
| 87 | auto event = reinterpret_cast<inotify_event*>(&buffer[offset]); |
| 88 | // Update the functional association on a RO |
| 89 | // active image symlink change |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 90 | std::filesystem::path path(PNOR_ACTIVE_PATH); |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 91 | path /= event->name; |
Brad Bishop | 9f44c99 | 2020-11-06 14:48:46 -0500 | [diff] [blame] | 92 | if (std::filesystem::equivalent(path, PNOR_RO_ACTIVE_PATH)) |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 93 | { |
Lei YU | bee5140 | 2019-02-26 11:36:34 +0800 | [diff] [blame] | 94 | auto id = ItemUpdaterUbi::determineId(path); |
Lei YU | f3ce433 | 2019-02-21 14:09:49 +0800 | [diff] [blame] | 95 | static_cast<Watch*>(userdata)->functionalCallback(id); |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 96 | } |
| 97 | offset += offsetof(inotify_event, name) + event->len; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | int Watch::inotifyInit() |
| 104 | { |
| 105 | auto fd = inotify_init1(IN_NONBLOCK); |
| 106 | |
| 107 | if (-1 == fd) |
| 108 | { |
| 109 | auto error = errno; |
Adriana Kobylak | 70dcb63 | 2018-02-27 15:46:52 -0600 | [diff] [blame] | 110 | throw std::system_error(error, std::generic_category(), |
Gunnar Mills | 6bd6d7b | 2017-09-18 09:22:36 -0500 | [diff] [blame] | 111 | "Error occurred during the inotify_init1"); |
| 112 | } |
| 113 | |
| 114 | return fd; |
| 115 | } |
| 116 | |
| 117 | } // namespace updater |
| 118 | } // namespace software |
| 119 | } // namespace openpower |