blob: 0fc61d82cf0e07197fb946dc6397c2b61028b680 [file] [log] [blame]
Adriana Kobylakb072d1b2018-04-24 11:37:21 -05001#include <experimental/filesystem>
2#include <fstream>
3#include <phosphor-logging/log.hpp>
4#include <sys/inotify.h>
5#include <unistd.h>
6#include "config.h"
7#include "sync_watch.hpp"
8
9namespace phosphor
10{
11namespace software
12{
13namespace manager
14{
15
16using namespace phosphor::logging;
17namespace fs = std::experimental::filesystem;
18
19SyncWatch::SyncWatch(sd_event& loop,
20 std::function<int(fs::path&)> syncCallback) :
21 syncCallback(syncCallback)
22{
23 auto syncfile = fs::path(SYNC_LIST_DIR_PATH) / SYNC_LIST_FILE_NAME;
24 if (fs::exists(syncfile))
25 {
26 std::string line;
27 std::ifstream file(syncfile.c_str());
28 while (std::getline(file, line))
29 {
30 auto fd = inotify_init1(IN_NONBLOCK);
31 if (-1 == fd)
32 {
33 log<level::ERR>("inotify_init1 failed",
34 entry("ERRNO=%d", errno),
35 entry("FILENAME=%s", line.c_str()),
36 entry("SYNCFILE=%s", syncfile.c_str()));
37 continue;
38 }
39
40 auto wd = inotify_add_watch(fd, line.c_str(), IN_CLOSE_WRITE);
41 if (-1 == wd)
42 {
43 log<level::ERR>("inotify_add_watch failed",
44 entry("ERRNO=%d", errno),
45 entry("FILENAME=%s", line.c_str()),
46 entry("SYNCFILE=%s", syncfile.c_str()));
47 close(fd);
48 continue;
49 }
50
51 auto rc =
52 sd_event_add_io(&loop, nullptr, fd, EPOLLIN, callback, this);
53 if (0 > rc)
54 {
55 log<level::ERR>("failed to add to event loop",
56 entry("RC=%d", rc),
57 entry("FILENAME=%s", line.c_str()),
58 entry("SYNCFILE=%s", syncfile.c_str()));
59 inotify_rm_watch(fd, wd);
60 close(fd);
61 continue;
62 }
63
64 fileMap[fd].insert(std::make_pair(wd, fs::path(line)));
65 }
66 }
67}
68
69SyncWatch::~SyncWatch()
70{
71 for (const auto& fd : fileMap)
72 {
73 for (const auto& wd : fd.second)
74 {
75 inotify_rm_watch(fd.first, wd.first);
76 }
77 close(fd.first);
78 }
79}
80
81int SyncWatch::callback(sd_event_source* s, int fd, uint32_t revents,
82 void* userdata)
83{
84 return 0;
85}
86
87} // namespace manager
88} // namespace software
89} // namespace phosphor