blob: 1bfc109885ee981cacf5ea15ebaf3145d5038ac9 [file] [log] [blame]
Marri Devender Raoffad1ef2019-06-03 04:54:12 -05001#include "watch.hpp"
2
Nan Zhou014be0b2021-12-28 18:00:14 -08003#include <sys/epoll.h>
Marri Devender Raoffad1ef2019-06-03 04:54:12 -05004#include <sys/inotify.h>
5#include <unistd.h>
6
Patrick Williams223e4602023-05-10 07:51:11 -05007#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/log.hpp>
10#include <sdeventplus/source/io.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
12
Nan Zhou014be0b2021-12-28 18:00:14 -080013#include <array>
14#include <cerrno>
15#include <climits>
16#include <cstdint>
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050017#include <cstring>
18#include <filesystem>
Nan Zhoue1289ad2021-12-28 11:02:56 -080019
20namespace phosphor::certs
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050021{
Nan Zhoucf06ccd2021-12-28 16:25:45 -080022
23using ::phosphor::logging::elog;
24using ::phosphor::logging::entry;
25using ::phosphor::logging::level;
26using ::phosphor::logging::log;
27using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050028namespace fs = std::filesystem;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050029
30Watch::Watch(sdeventplus::Event& event, std::string& certFile, Callback cb) :
Nan Zhou014be0b2021-12-28 18:00:14 -080031 event(event), callback(std::move(cb))
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050032{
33 // get parent directory of certificate file to watch
Nan Zhoucf06ccd2021-12-28 16:25:45 -080034 fs::path path = fs::path(certFile).parent_path();
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050035 try
36 {
37 if (!fs::exists(path))
38 {
39 fs::create_directories(path);
40 }
41 }
Patrick Williams71957992021-10-06 14:42:52 -050042 catch (const fs::filesystem_error& e)
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050043 {
44 log<level::ERR>("Failed to create directory", entry("ERR=%s", e.what()),
45 entry("DIRECTORY=%s", path.c_str()));
46 elog<InternalFailure>();
47 }
48 watchDir = path;
49 watchFile = fs::path(certFile).filename();
50 startWatch();
51}
52
53Watch::~Watch()
54{
55 stopWatch();
56}
57
58void Watch::startWatch()
59{
60 // stop any existing watch
61 stopWatch();
62
63 fd = inotify_init1(IN_NONBLOCK);
64 if (-1 == fd)
65 {
66 log<level::ERR>("inotify_init1 failed,",
67 entry("ERR=%s", std::strerror(errno)));
68 elog<InternalFailure>();
69 }
70 wd = inotify_add_watch(fd, watchDir.c_str(), IN_CLOSE_WRITE);
71 if (-1 == wd)
72 {
73 close(fd);
74 log<level::ERR>("inotify_add_watch failed,",
75 entry("ERR=%s", std::strerror(errno)),
76 entry("WATCH=%s", watchDir.c_str()));
77 elog<InternalFailure>();
78 }
79
80 ioPtr = std::make_unique<sdeventplus::source::IO>(
Patrick Williams7e2797e2021-12-03 13:37:07 -060081 event, fd, EPOLLIN, [this](sdeventplus::source::IO&, int fd, uint32_t) {
Patrick Williams13eba412021-12-03 16:03:43 -060082 constexpr int size = sizeof(struct inotify_event) + NAME_MAX + 1;
83 std::array<char, size> buffer{};
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050084 int length = read(fd, buffer.data(), buffer.size());
85 if (length >= static_cast<int>(sizeof(struct inotify_event)))
86 {
87 struct inotify_event* notifyEvent =
88 reinterpret_cast<struct inotify_event*>(&buffer[0]);
89 if (notifyEvent->len)
90 {
91 if (watchFile == notifyEvent->name)
92 {
93 callback();
94 }
95 }
96 }
97 else
98 {
99 log<level::ERR>("Failed to read inotify event");
100 }
101 });
102}
103
104void Watch::stopWatch()
105{
106 if (-1 != fd)
107 {
108 if (-1 != wd)
109 {
110 inotify_rm_watch(fd, wd);
111 }
112 close(fd);
113 }
114 if (ioPtr)
115 {
116 ioPtr.reset();
117 }
118}
119
Nan Zhoue1289ad2021-12-28 11:02:56 -0800120} // namespace phosphor::certs