watch.cpp (223e460421eebb1c598d9285b0cb01f1150fa50d) watch.cpp (f2646271e5fc66e4c5f3f8bfd6eeb68b6be3f103)
1#include "watch.hpp"
2
3#include <sys/epoll.h>
4#include <sys/inotify.h>
5#include <unistd.h>
6
7#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/elog.hpp>
1#include "watch.hpp"
2
3#include <sys/epoll.h>
4#include <sys/inotify.h>
5#include <unistd.h>
6
7#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/log.hpp>
9#include <phosphor-logging/lg2.hpp>
10#include <sdeventplus/source/io.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
12
13#include <array>
14#include <cerrno>
15#include <climits>
16#include <cstdint>
17#include <cstring>
18#include <filesystem>
19
20namespace phosphor::certs
21{
22
23using ::phosphor::logging::elog;
10#include <sdeventplus/source/io.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
12
13#include <array>
14#include <cerrno>
15#include <climits>
16#include <cstdint>
17#include <cstring>
18#include <filesystem>
19
20namespace phosphor::certs
21{
22
23using ::phosphor::logging::elog;
24using ::phosphor::logging::entry;
25using ::phosphor::logging::level;
26using ::phosphor::logging::log;
27using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
28namespace fs = std::filesystem;
29
30Watch::Watch(sdeventplus::Event& event, std::string& certFile, Callback cb) :
31 event(event), callback(std::move(cb))
32{
33 // get parent directory of certificate file to watch
34 fs::path path = fs::path(certFile).parent_path();
35 try
36 {
37 if (!fs::exists(path))
38 {
39 fs::create_directories(path);
40 }
41 }
42 catch (const fs::filesystem_error& e)
43 {
24using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
25namespace fs = std::filesystem;
26
27Watch::Watch(sdeventplus::Event& event, std::string& certFile, Callback cb) :
28 event(event), callback(std::move(cb))
29{
30 // get parent directory of certificate file to watch
31 fs::path path = fs::path(certFile).parent_path();
32 try
33 {
34 if (!fs::exists(path))
35 {
36 fs::create_directories(path);
37 }
38 }
39 catch (const fs::filesystem_error& e)
40 {
44 log<level::ERR>("Failed to create directory", entry("ERR=%s", e.what()),
45 entry("DIRECTORY=%s", path.c_str()));
41 lg2::error(
42 "Failed to create directory, ERR:{ERR}, DIRECTORY:{DIRECTORY}",
43 "ERR", e, "DIRECTORY", path);
46 elog<InternalFailure>();
47 }
48 watchDir = path;
49 watchFile = fs::path(certFile).filename();
50 startWatch();
51}
52
53Watch::~Watch()

--- 4 unchanged lines hidden (view full) ---

58void Watch::startWatch()
59{
60 // stop any existing watch
61 stopWatch();
62
63 fd = inotify_init1(IN_NONBLOCK);
64 if (-1 == fd)
65 {
44 elog<InternalFailure>();
45 }
46 watchDir = path;
47 watchFile = fs::path(certFile).filename();
48 startWatch();
49}
50
51Watch::~Watch()

--- 4 unchanged lines hidden (view full) ---

56void Watch::startWatch()
57{
58 // stop any existing watch
59 stopWatch();
60
61 fd = inotify_init1(IN_NONBLOCK);
62 if (-1 == fd)
63 {
66 log<level::ERR>("inotify_init1 failed,",
67 entry("ERR=%s", std::strerror(errno)));
64 lg2::error("inotify_init1 failed: {ERR}", "ERR", 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);
65 elog<InternalFailure>();
66 }
67 wd = inotify_add_watch(fd, watchDir.c_str(), IN_CLOSE_WRITE);
68 if (-1 == wd)
69 {
70 close(fd);
74 log<level::ERR>("inotify_add_watch failed,",
75 entry("ERR=%s", std::strerror(errno)),
76 entry("WATCH=%s", watchDir.c_str()));
71 lg2::error("inotify_add_watch failed, ERR:{ERR}, WATCH:{WATCH}", "ERR",
72 std::strerror(errno), "WATCH", watchDir);
77 elog<InternalFailure>();
78 }
79
80 ioPtr = std::make_unique<sdeventplus::source::IO>(
81 event, fd, EPOLLIN, [this](sdeventplus::source::IO&, int fd, uint32_t) {
82 constexpr int size = sizeof(struct inotify_event) + NAME_MAX + 1;
83 std::array<char, size> buffer{};
84 int length = read(fd, buffer.data(), buffer.size());

--- 6 unchanged lines hidden (view full) ---

91 if (watchFile == notifyEvent->name)
92 {
93 callback();
94 }
95 }
96 }
97 else
98 {
73 elog<InternalFailure>();
74 }
75
76 ioPtr = std::make_unique<sdeventplus::source::IO>(
77 event, fd, EPOLLIN, [this](sdeventplus::source::IO&, int fd, uint32_t) {
78 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());

--- 6 unchanged lines hidden (view full) ---

87 if (watchFile == notifyEvent->name)
88 {
89 callback();
90 }
91 }
92 }
93 else
94 {
99 log<level::ERR>("Failed to read inotify event");
95 lg2::error("Failed to read inotify event");
100 }
101 });
102}
103
104void Watch::stopWatch()
105{
106 if (-1 != fd)
107 {

--- 13 unchanged lines hidden ---
96 }
97 });
98}
99
100void Watch::stopWatch()
101{
102 if (-1 != fd)
103 {

--- 13 unchanged lines hidden ---