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