blob: 18c4dfd39b0a303b263e0042f329e091c3d76783 [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>
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -05006#include <phosphor-logging/lg2.hpp>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05007
8namespace phosphor
9{
10namespace dump
11{
12namespace inotify
13{
14
15using namespace std::string_literals;
16using namespace phosphor::logging;
17using namespace sdbusplus::xyz::openbmc_project::Common::Error;
18
19Watch::~Watch()
20{
21 if ((fd() >= 0) && (wd >= 0))
22 {
23 inotify_rm_watch(fd(), wd);
24 }
25}
26
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050027Watch::Watch(const EventPtr& eventObj, const int flags, const uint32_t mask,
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050028 const uint32_t events, const std::filesystem::path& path,
29 UserType userFunc) :
Patrick Williams973b2912024-08-16 15:20:50 -040030 flags(flags), mask(mask), events(events), path(path), fd(inotifyInit()),
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050031 userFunc(userFunc)
32{
33 // Check if watch DIR exists.
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050034 if (!std::filesystem::is_directory(path))
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050035 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050036 lg2::error("Watch directory doesn't exist, DIR: {DIRECTORY}",
37 "DIRECTORY", path);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050038 elog<InternalFailure>();
39 }
40
41 wd = inotify_add_watch(fd(), path.c_str(), mask);
42 if (-1 == wd)
43 {
44 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050045 lg2::error(
46 "Error occurred during the inotify_add_watch call, errno: {ERRNO}",
47 "ERRNO", error);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050048 elog<InternalFailure>();
49 }
50
Patrick Williams973b2912024-08-16 15:20:50 -040051 auto rc =
52 sd_event_add_io(eventObj.get(), nullptr, fd(), events, callback, this);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050053 if (0 > rc)
54 {
55 // Failed to add to event loop
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050056 lg2::error("Error occurred during the sd_event_add_io call, rc: {RC}",
57 "RC", rc);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050058 elog<InternalFailure>();
59 }
60}
61
62int Watch::inotifyInit()
63{
64 auto fd = inotify_init1(flags);
65
66 if (-1 == fd)
67 {
68 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050069 lg2::error("Error occurred during the inotify_init1, errno: {ERRNO}",
70 "ERRNO", error);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050071 elog<InternalFailure>();
72 }
73
74 return fd;
75}
76
Ramesh Iyyarbb410df2020-08-03 03:13:04 -050077int Watch::callback(sd_event_source*, int fd, uint32_t revents, void* userdata)
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050078{
Jayanth Othayoth764d1b22017-07-12 19:14:02 -050079 auto userData = static_cast<Watch*>(userdata);
80
81 if (!(revents & userData->events))
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050082 {
83 return 0;
84 }
85
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050086 // Maximum inotify events supported in the buffer
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050087 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 Othayothcb65ffc2018-10-16 08:29:32 -050093 // Failed to read inotify event
94 // Report error and return
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050095 auto error = errno;
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050096 lg2::error("Error occurred during the read, errno: {ERRNO}", "ERRNO",
97 error);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050098 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 Othayoth764d1b22017-07-12 19:14:02 -0500109 auto mask = event->mask & userData->mask;
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500110
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500111 if (mask)
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500112 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500113 userMap.emplace((userData->path / event->name), mask);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500114 }
115
116 offset += offsetof(inotify_event, name) + event->len;
117 }
118
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500119 // Call user call back function in case valid data in the map
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500120 if (!userMap.empty())
121 {
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500122 userData->userFunc(userMap);
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -0500123 }
124
125 return 0;
126}
127
128} // namespace inotify
129} // namespace dump
130} // namespace phosphor