forked from ibm-openbmc/phosphor-certificate-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.cpp
116 lines (104 loc) · 2.79 KB
/
watch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include "watch.hpp"
#include <sys/epoll.h>
#include <sys/inotify.h>
#include <unistd.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdeventplus/source/io.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <array>
#include <cerrno>
#include <climits>
#include <cstdint>
#include <cstring>
#include <filesystem>
namespace phosphor::certs
{
using ::phosphor::logging::elog;
using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
namespace fs = std::filesystem;
Watch::Watch(sdeventplus::Event& event, std::string& certFile, Callback cb) :
event(event), callback(std::move(cb))
{
// get parent directory of certificate file to watch
fs::path path = fs::path(certFile).parent_path();
try
{
if (!fs::exists(path))
{
fs::create_directories(path);
}
}
catch (const fs::filesystem_error& e)
{
lg2::error(
"Failed to create directory, ERR:{ERR}, DIRECTORY:{DIRECTORY}",
"ERR", e, "DIRECTORY", path);
elog<InternalFailure>();
}
watchDir = path;
watchFile = fs::path(certFile).filename();
startWatch();
}
Watch::~Watch()
{
stopWatch();
}
void Watch::startWatch()
{
// stop any existing watch
stopWatch();
fd = inotify_init1(IN_NONBLOCK);
if (-1 == fd)
{
lg2::error("inotify_init1 failed: {ERR}", "ERR", std::strerror(errno));
elog<InternalFailure>();
}
wd = inotify_add_watch(fd, watchDir.c_str(), IN_CLOSE_WRITE);
if (-1 == wd)
{
close(fd);
lg2::error("inotify_add_watch failed, ERR:{ERR}, WATCH:{WATCH}", "ERR",
std::strerror(errno), "WATCH", watchDir);
elog<InternalFailure>();
}
ioPtr = std::make_unique<sdeventplus::source::IO>(
event, fd, EPOLLIN, [this](sdeventplus::source::IO&, int fd, uint32_t) {
constexpr int size = sizeof(struct inotify_event) + NAME_MAX + 1;
std::array<char, size> buffer{};
int length = read(fd, buffer.data(), buffer.size());
if (length >= static_cast<int>(sizeof(struct inotify_event)))
{
struct inotify_event* notifyEvent =
reinterpret_cast<struct inotify_event*>(&buffer[0]);
if (notifyEvent->len)
{
if (watchFile == notifyEvent->name)
{
callback();
}
}
}
else
{
lg2::error("Failed to read inotify event");
}
});
}
void Watch::stopWatch()
{
if (-1 != fd)
{
if (-1 != wd)
{
inotify_rm_watch(fd, wd);
}
close(fd);
}
if (ioPtr)
{
ioPtr.reset();
}
}
} // namespace phosphor::certs