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