blob: e7852658049500098db9ebdbba7298f072c364ee [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>
Ravi Tejaf2646272023-09-30 13:00:55 -05009#include <phosphor-logging/lg2.hpp>
Patrick Williams223e4602023-05-10 07:51:11 -050010#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;
Nan Zhoucf06ccd2021-12-28 16:25:45 -080024using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050025namespace fs = std::filesystem;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050026
27Watch::Watch(sdeventplus::Event& event, std::string& certFile, Callback cb) :
Nan Zhou014be0b2021-12-28 18:00:14 -080028 event(event), callback(std::move(cb))
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050029{
30 // get parent directory of certificate file to watch
Nan Zhoucf06ccd2021-12-28 16:25:45 -080031 fs::path path = fs::path(certFile).parent_path();
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050032 try
33 {
34 if (!fs::exists(path))
35 {
36 fs::create_directories(path);
37 }
38 }
Patrick Williams71957992021-10-06 14:42:52 -050039 catch (const fs::filesystem_error& e)
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050040 {
Ravi Tejaf2646272023-09-30 13:00:55 -050041 lg2::error(
42 "Failed to create directory, ERR:{ERR}, DIRECTORY:{DIRECTORY}",
43 "ERR", e, "DIRECTORY", path);
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050044 elog<InternalFailure>();
45 }
46 watchDir = path;
47 watchFile = fs::path(certFile).filename();
48 startWatch();
49}
50
51Watch::~Watch()
52{
53 stopWatch();
54}
55
56void Watch::startWatch()
57{
58 // stop any existing watch
59 stopWatch();
60
61 fd = inotify_init1(IN_NONBLOCK);
62 if (-1 == fd)
63 {
Ravi Tejaf2646272023-09-30 13:00:55 -050064 lg2::error("inotify_init1 failed: {ERR}", "ERR", std::strerror(errno));
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050065 elog<InternalFailure>();
66 }
67 wd = inotify_add_watch(fd, watchDir.c_str(), IN_CLOSE_WRITE);
68 if (-1 == wd)
69 {
70 close(fd);
Ravi Tejaf2646272023-09-30 13:00:55 -050071 lg2::error("inotify_add_watch failed, ERR:{ERR}, WATCH:{WATCH}", "ERR",
72 std::strerror(errno), "WATCH", watchDir);
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050073 elog<InternalFailure>();
74 }
75
76 ioPtr = std::make_unique<sdeventplus::source::IO>(
Patrick Williams7e2797e2021-12-03 13:37:07 -060077 event, fd, EPOLLIN, [this](sdeventplus::source::IO&, int fd, uint32_t) {
Patrick Williamsd96b81c2023-10-20 11:19:39 -050078 constexpr int size = sizeof(struct inotify_event) + NAME_MAX + 1;
79 std::array<char, size> buffer{};
80 int length = read(fd, buffer.data(), buffer.size());
81 if (length >= static_cast<int>(sizeof(struct inotify_event)))
82 {
83 struct inotify_event* notifyEvent =
84 reinterpret_cast<struct inotify_event*>(&buffer[0]);
85 if (notifyEvent->len)
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050086 {
Patrick Williamsd96b81c2023-10-20 11:19:39 -050087 if (watchFile == notifyEvent->name)
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050088 {
Patrick Williamsd96b81c2023-10-20 11:19:39 -050089 callback();
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050090 }
91 }
Patrick Williamsd96b81c2023-10-20 11:19:39 -050092 }
93 else
94 {
95 lg2::error("Failed to read inotify event");
96 }
97 });
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050098}
99
100void Watch::stopWatch()
101{
102 if (-1 != fd)
103 {
104 if (-1 != wd)
105 {
106 inotify_rm_watch(fd, wd);
107 }
108 close(fd);
109 }
110 if (ioPtr)
111 {
112 ioPtr.reset();
113 }
114}
115
Nan Zhoue1289ad2021-12-28 11:02:56 -0800116} // namespace phosphor::certs