summaryrefslogtreecommitdiffstats
path: root/sync_watch.cpp
diff options
context:
space:
mode:
authorAdriana Kobylak <anoo@us.ibm.com>2018-04-24 11:37:21 -0500
committerAdriana Kobylak <anoo@us.ibm.com>2018-05-08 11:51:36 -0500
commitb072d1bec4f8c5ee2de13722a4af63ba4350d7c7 (patch)
tree86d93a28745daacd5518c1888e82f9879cfcdf96 /sync_watch.cpp
parent7e8d76b3de932520789a8fb2920cd4b6fde9d388 (diff)
downloadphosphor-bmc-code-mgmt-b072d1bec4f8c5ee2de13722a4af63ba4350d7c7.tar.gz
phosphor-bmc-code-mgmt-b072d1bec4f8c5ee2de13722a4af63ba4350d7c7.zip
sync_manager: Create sync watch class
Create a watch class to monitor the files and directories specified in the synclist file. Store the file descriptors and file names in a map to be able to know the full path of the file that triggered the event. The watch descriptor number does not change so it can be a single variable. Change-Id: I211225ddc012af85d9be39ae5d40b8258d73435d Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
Diffstat (limited to 'sync_watch.cpp')
-rw-r--r--sync_watch.cpp89
1 files changed, 89 insertions, 0 deletions
diff --git a/sync_watch.cpp b/sync_watch.cpp
new file mode 100644
index 0000000..0fc61d8
--- /dev/null
+++ b/sync_watch.cpp
@@ -0,0 +1,89 @@
+#include <experimental/filesystem>
+#include <fstream>
+#include <phosphor-logging/log.hpp>
+#include <sys/inotify.h>
+#include <unistd.h>
+#include "config.h"
+#include "sync_watch.hpp"
+
+namespace phosphor
+{
+namespace software
+{
+namespace manager
+{
+
+using namespace phosphor::logging;
+namespace fs = std::experimental::filesystem;
+
+SyncWatch::SyncWatch(sd_event& loop,
+ std::function<int(fs::path&)> syncCallback) :
+ syncCallback(syncCallback)
+{
+ auto syncfile = fs::path(SYNC_LIST_DIR_PATH) / SYNC_LIST_FILE_NAME;
+ if (fs::exists(syncfile))
+ {
+ std::string line;
+ std::ifstream file(syncfile.c_str());
+ while (std::getline(file, line))
+ {
+ auto fd = inotify_init1(IN_NONBLOCK);
+ if (-1 == fd)
+ {
+ log<level::ERR>("inotify_init1 failed",
+ entry("ERRNO=%d", errno),
+ entry("FILENAME=%s", line.c_str()),
+ entry("SYNCFILE=%s", syncfile.c_str()));
+ continue;
+ }
+
+ auto wd = inotify_add_watch(fd, line.c_str(), IN_CLOSE_WRITE);
+ if (-1 == wd)
+ {
+ log<level::ERR>("inotify_add_watch failed",
+ entry("ERRNO=%d", errno),
+ entry("FILENAME=%s", line.c_str()),
+ entry("SYNCFILE=%s", syncfile.c_str()));
+ close(fd);
+ continue;
+ }
+
+ auto rc =
+ sd_event_add_io(&loop, nullptr, fd, EPOLLIN, callback, this);
+ if (0 > rc)
+ {
+ log<level::ERR>("failed to add to event loop",
+ entry("RC=%d", rc),
+ entry("FILENAME=%s", line.c_str()),
+ entry("SYNCFILE=%s", syncfile.c_str()));
+ inotify_rm_watch(fd, wd);
+ close(fd);
+ continue;
+ }
+
+ fileMap[fd].insert(std::make_pair(wd, fs::path(line)));
+ }
+ }
+}
+
+SyncWatch::~SyncWatch()
+{
+ for (const auto& fd : fileMap)
+ {
+ for (const auto& wd : fd.second)
+ {
+ inotify_rm_watch(fd.first, wd.first);
+ }
+ close(fd.first);
+ }
+}
+
+int SyncWatch::callback(sd_event_source* s, int fd, uint32_t revents,
+ void* userdata)
+{
+ return 0;
+}
+
+} // namespace manager
+} // namespace software
+} // namespace phosphor
OpenPOWER on IntegriCloud