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