blob: 659320a98e4994530726d40fdee7af3d90c3f6d8 [file] [log] [blame]
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05001#include "watch.hpp"
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05002
3#include "xyz/openbmc_project/Common/error.hpp"
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05004
5#include <phosphor-logging/elog-errors.hpp>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05006
7namespace phosphor
8{
9namespace dump
10{
11namespace inotify
12{
13
14using namespace std::string_literals;
15using namespace phosphor::logging;
16using namespace sdbusplus::xyz::openbmc_project::Common::Error;
17
18Watch::~Watch()
19{
20 if ((fd() >= 0) && (wd >= 0))
21 {
22 inotify_rm_watch(fd(), wd);
23 }
24}
25
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050026Watch::Watch(const EventPtr& eventObj, const int flags, const uint32_t mask,
27 const uint32_t events, const fs::path& path, UserType userFunc) :
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050028 flags(flags),
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050029 mask(mask), events(events), path(path), fd(inotifyInit()),
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050030 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 Mills11eaab72017-10-19 16:07:31 -050036 entry("DIR=%s", path.c_str()));
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050037 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 Othayothcb65ffc2018-10-16 08:29:32 -050049 auto rc =
50 sd_event_add_io(eventObj.get(), nullptr, fd(), events, callback, this);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050051 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 Mills11eaab72017-10-19 16:07:31 -050055 entry("RC=%d", rc));
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050056 elog<InternalFailure>();
57 }
58}
59
60int 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 Iyyarbb410df2020-08-03 03:13:04 -050075int Watch::callback(sd_event_source*, int fd, uint32_t revents, void* userdata)
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050076{
Jayanth Othayoth764d1b22017-07-12 19:14:02 -050077 auto userData = static_cast<Watch*>(userdata);
78
79 if (!(revents & userData->events))
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050080 {
81 return 0;
82 }
83
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050084 // Maximum inotify events supported in the buffer
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050085 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 Othayothcb65ffc2018-10-16 08:29:32 -050091 // Failed to read inotify event
92 // Report error and return
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050093 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 Othayoth764d1b22017-07-12 19:14:02 -0500107 auto mask = event->mask & userData->mask;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500108
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500109 if (mask)
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500110 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500111 userMap.emplace((userData->path / event->name), mask);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500112 }
113
114 offset += offsetof(inotify_event, name) + event->len;
115 }
116
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500117 // Call user call back function in case valid data in the map
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500118 if (!userMap.empty())
119 {
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500120 userData->userFunc(userMap);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500121 }
122
123 return 0;
124}
125
126} // namespace inotify
127} // namespace dump
128} // namespace phosphor