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