blob: a39d4926bf3de065caedcf99a422d6585a12a2fb [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
Nan Zhou014be0b2021-12-28 18:00:14 -08007#include <array>
8#include <cerrno>
9#include <climits>
10#include <cstdint>
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050011#include <cstring>
12#include <filesystem>
13#include <phosphor-logging/elog-errors.hpp>
14#include <phosphor-logging/elog.hpp>
Nan Zhou014be0b2021-12-28 18:00:14 -080015#include <phosphor-logging/log.hpp>
16#include <sdeventplus/source/io.hpp>
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050017#include <xyz/openbmc_project/Common/error.hpp>
Nan Zhoue1289ad2021-12-28 11:02:56 -080018
19namespace phosphor::certs
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050020{
Nan Zhoucf06ccd2021-12-28 16:25:45 -080021
22using ::phosphor::logging::elog;
23using ::phosphor::logging::entry;
24using ::phosphor::logging::level;
25using ::phosphor::logging::log;
26using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050027namespace fs = std::filesystem;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050028
29Watch::Watch(sdeventplus::Event& event, std::string& certFile, Callback cb) :
Nan Zhou014be0b2021-12-28 18:00:14 -080030 event(event), callback(std::move(cb))
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050031{
32 // get parent directory of certificate file to watch
Nan Zhoucf06ccd2021-12-28 16:25:45 -080033 fs::path path = fs::path(certFile).parent_path();
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050034 try
35 {
36 if (!fs::exists(path))
37 {
38 fs::create_directories(path);
39 }
40 }
Patrick Williams71957992021-10-06 14:42:52 -050041 catch (const fs::filesystem_error& e)
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050042 {
43 log<level::ERR>("Failed to create directory", entry("ERR=%s", e.what()),
44 entry("DIRECTORY=%s", path.c_str()));
45 elog<InternalFailure>();
46 }
47 watchDir = path;
48 watchFile = fs::path(certFile).filename();
49 startWatch();
50}
51
52Watch::~Watch()
53{
54 stopWatch();
55}
56
57void Watch::startWatch()
58{
59 // stop any existing watch
60 stopWatch();
61
62 fd = inotify_init1(IN_NONBLOCK);
63 if (-1 == fd)
64 {
65 log<level::ERR>("inotify_init1 failed,",
66 entry("ERR=%s", std::strerror(errno)));
67 elog<InternalFailure>();
68 }
69 wd = inotify_add_watch(fd, watchDir.c_str(), IN_CLOSE_WRITE);
70 if (-1 == wd)
71 {
72 close(fd);
73 log<level::ERR>("inotify_add_watch failed,",
74 entry("ERR=%s", std::strerror(errno)),
75 entry("WATCH=%s", watchDir.c_str()));
76 elog<InternalFailure>();
77 }
78
79 ioPtr = std::make_unique<sdeventplus::source::IO>(
Patrick Williams7e2797e2021-12-03 13:37:07 -060080 event, fd, EPOLLIN, [this](sdeventplus::source::IO&, int fd, uint32_t) {
Patrick Williams13eba412021-12-03 16:03:43 -060081 constexpr int size = sizeof(struct inotify_event) + NAME_MAX + 1;
82 std::array<char, size> buffer{};
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050083 int length = read(fd, buffer.data(), buffer.size());
84 if (length >= static_cast<int>(sizeof(struct inotify_event)))
85 {
86 struct inotify_event* notifyEvent =
87 reinterpret_cast<struct inotify_event*>(&buffer[0]);
88 if (notifyEvent->len)
89 {
90 if (watchFile == notifyEvent->name)
91 {
92 callback();
93 }
94 }
95 }
96 else
97 {
98 log<level::ERR>("Failed to read inotify event");
99 }
100 });
101}
102
103void Watch::stopWatch()
104{
105 if (-1 != fd)
106 {
107 if (-1 != wd)
108 {
109 inotify_rm_watch(fd, wd);
110 }
111 close(fd);
112 }
113 if (ioPtr)
114 {
115 ioPtr.reset();
116 }
117}
118
Nan Zhoue1289ad2021-12-28 11:02:56 -0800119} // namespace phosphor::certs