watch.cpp (f2646271e5fc66e4c5f3f8bfd6eeb68b6be3f103) watch.cpp (d96b81cac9113a55e98281fda983c64fd7d83c7d)
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>

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

70 close(fd);
71 lg2::error("inotify_add_watch failed, ERR:{ERR}, WATCH:{WATCH}", "ERR",
72 std::strerror(errno), "WATCH", watchDir);
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) {
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>

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

70 close(fd);
71 lg2::error("inotify_add_watch failed, ERR:{ERR}, WATCH:{WATCH}", "ERR",
72 std::strerror(errno), "WATCH", watchDir);
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());
81 if (length >= static_cast<int>(sizeof(struct inotify_event)))
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());
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)
82 {
86 {
83 struct inotify_event* notifyEvent =
84 reinterpret_cast<struct inotify_event*>(&buffer[0]);
85 if (notifyEvent->len)
87 if (watchFile == notifyEvent->name)
86 {
88 {
87 if (watchFile == notifyEvent->name)
88 {
89 callback();
90 }
89 callback();
91 }
92 }
90 }
91 }
93 else
94 {
95 lg2::error("Failed to read inotify event");
96 }
97 });
92 }
93 else
94 {
95 lg2::error("Failed to read inotify event");
96 }
97 });
98}
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
116} // namespace phosphor::certs
98}
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
116} // namespace phosphor::certs