Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 1 | #include "watch.hpp" |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 2 | |
| 3 | #include "xyz/openbmc_project/Common/error.hpp" |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 4 | |
| 5 | #include <phosphor-logging/elog-errors.hpp> |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 6 | #include <phosphor-logging/lg2.hpp> |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 7 | |
Jayanth Othayoth | ceb3e76 | 2024-11-25 23:19:37 -0600 | [diff] [blame] | 8 | #include <span> |
| 9 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 10 | namespace phosphor |
| 11 | { |
| 12 | namespace dump |
| 13 | { |
| 14 | namespace inotify |
| 15 | { |
| 16 | |
| 17 | using namespace std::string_literals; |
| 18 | using namespace phosphor::logging; |
| 19 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 20 | |
| 21 | Watch::~Watch() |
| 22 | { |
| 23 | if ((fd() >= 0) && (wd >= 0)) |
| 24 | { |
| 25 | inotify_rm_watch(fd(), wd); |
| 26 | } |
| 27 | } |
| 28 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 29 | Watch::Watch(const EventPtr& eventObj, const int flags, const uint32_t mask, |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 30 | const uint32_t events, const std::filesystem::path& path, |
| 31 | UserType userFunc) : |
Patrick Williams | 973b291 | 2024-08-16 15:20:50 -0400 | [diff] [blame] | 32 | flags(flags), mask(mask), events(events), path(path), fd(inotifyInit()), |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 33 | userFunc(userFunc) |
| 34 | { |
| 35 | // Check if watch DIR exists. |
Jayanth Othayoth | 3fc6df4 | 2021-04-08 03:45:24 -0500 | [diff] [blame] | 36 | if (!std::filesystem::is_directory(path)) |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 37 | { |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 38 | lg2::error("Watch directory doesn't exist, DIR: {DIRECTORY}", |
| 39 | "DIRECTORY", path); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 40 | elog<InternalFailure>(); |
| 41 | } |
| 42 | |
| 43 | wd = inotify_add_watch(fd(), path.c_str(), mask); |
| 44 | if (-1 == wd) |
| 45 | { |
| 46 | auto error = errno; |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 47 | lg2::error( |
| 48 | "Error occurred during the inotify_add_watch call, errno: {ERRNO}", |
| 49 | "ERRNO", error); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 50 | elog<InternalFailure>(); |
| 51 | } |
| 52 | |
Patrick Williams | 973b291 | 2024-08-16 15:20:50 -0400 | [diff] [blame] | 53 | auto rc = |
| 54 | sd_event_add_io(eventObj.get(), nullptr, fd(), events, callback, this); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 55 | if (0 > rc) |
| 56 | { |
| 57 | // Failed to add to event loop |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 58 | lg2::error("Error occurred during the sd_event_add_io call, rc: {RC}", |
| 59 | "RC", rc); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 60 | elog<InternalFailure>(); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | int Watch::inotifyInit() |
| 65 | { |
| 66 | auto fd = inotify_init1(flags); |
| 67 | |
| 68 | if (-1 == fd) |
| 69 | { |
| 70 | auto error = errno; |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 71 | lg2::error("Error occurred during the inotify_init1, errno: {ERRNO}", |
| 72 | "ERRNO", error); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 73 | elog<InternalFailure>(); |
| 74 | } |
| 75 | |
| 76 | return fd; |
| 77 | } |
| 78 | |
Ramesh Iyyar | bb410df | 2020-08-03 03:13:04 -0500 | [diff] [blame] | 79 | int Watch::callback(sd_event_source*, int fd, uint32_t revents, void* userdata) |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 80 | { |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 81 | auto userData = static_cast<Watch*>(userdata); |
| 82 | |
Jayanth Othayoth | b913e07 | 2024-11-26 01:23:20 -0600 | [diff] [blame^] | 83 | if ((revents & userData->events) == 0U) |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 88 | // Maximum inotify events supported in the buffer |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 89 | constexpr auto maxBytes = sizeof(struct inotify_event) + NAME_MAX + 1; |
| 90 | uint8_t buffer[maxBytes]; |
| 91 | |
Jayanth Othayoth | ceb3e76 | 2024-11-25 23:19:37 -0600 | [diff] [blame] | 92 | std::span<char> bufferSpan(reinterpret_cast<char*>(buffer), maxBytes); |
| 93 | auto bytes = read(fd, bufferSpan.data(), bufferSpan.size()); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 94 | if (0 > bytes) |
| 95 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 96 | // Failed to read inotify event |
| 97 | // Report error and return |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 98 | auto error = errno; |
Dhruvaraj Subhashchandran | d1f670f | 2023-06-05 22:19:25 -0500 | [diff] [blame] | 99 | lg2::error("Error occurred during the read, errno: {ERRNO}", "ERRNO", |
| 100 | error); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 101 | report<InternalFailure>(); |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | auto offset = 0; |
| 106 | |
| 107 | UserMap userMap; |
| 108 | |
| 109 | while (offset < bytes) |
| 110 | { |
| 111 | auto event = reinterpret_cast<inotify_event*>(&buffer[offset]); |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 112 | auto mask = event->mask & userData->mask; |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 113 | |
Jayanth Othayoth | b913e07 | 2024-11-26 01:23:20 -0600 | [diff] [blame^] | 114 | if (mask != 0U) |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 115 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 116 | userMap.emplace((userData->path / event->name), mask); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | offset += offsetof(inotify_event, name) + event->len; |
| 120 | } |
| 121 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 122 | // Call user call back function in case valid data in the map |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 123 | if (!userMap.empty()) |
| 124 | { |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 125 | userData->userFunc(userMap); |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | } // namespace inotify |
| 132 | } // namespace dump |
| 133 | } // namespace phosphor |