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