blob: 7f1f97093f8340e3d67a11df6324157911047db6 [file] [log] [blame]
Gunnar Millsb0ce9962018-09-07 13:39:10 -05001#include "config.h"
2
3#include "sync_watch.hpp"
4
5#include <sys/inotify.h>
6#include <unistd.h>
7
Adriana Kobylak58aa7502020-06-08 11:12:11 -05008#include <phosphor-logging/log.hpp>
9
Adriana Kobylakc98d9122020-05-05 10:36:01 -050010#include <filesystem>
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050011#include <fstream>
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050012
13namespace phosphor
14{
15namespace software
16{
17namespace manager
18{
19
20using namespace phosphor::logging;
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050021
Adriana Kobylak3a19e622018-11-09 11:39:46 -060022void SyncWatch::addInotifyWatch(const fs::path& path)
23{
Brad Bishop02516d32018-11-19 15:29:26 -050024 auto wd =
25 inotify_add_watch(inotifyFd, path.c_str(), IN_CLOSE_WRITE | IN_DELETE);
Adriana Kobylak3a19e622018-11-09 11:39:46 -060026 if (-1 == wd)
27 {
28 log<level::ERR>("inotify_add_watch failed", entry("ERRNO=%d", errno),
29 entry("FILENAME=%s", path.c_str()));
Adriana Kobylak3a19e622018-11-09 11:39:46 -060030 return;
31 }
32
Brad Bishop02516d32018-11-19 15:29:26 -050033 fileMap[wd] = fs::path(path);
Adriana Kobylak3a19e622018-11-09 11:39:46 -060034}
35
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050036SyncWatch::SyncWatch(sd_event& loop,
Adriana Kobylaka9074342018-05-08 11:52:44 -050037 std::function<int(int, fs::path&)> syncCallback) :
Brad Bishop02516d32018-11-19 15:29:26 -050038 inotifyFd(-1),
39 syncCallback(syncCallback), loop(loop)
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050040{
Brad Bishop02516d32018-11-19 15:29:26 -050041 auto fd = inotify_init1(IN_NONBLOCK);
42 if (-1 == fd)
43 {
44 log<level::ERR>("inotify_init1 failed", entry("ERRNO=%d", errno));
45 return;
46 }
47 inotifyFd = fd;
48
49 auto rc = sd_event_add_io(&loop, nullptr, fd, EPOLLIN, callback, this);
50 if (0 > rc)
51 {
52 log<level::ERR>("failed to add to event loop", entry("RC=%d", rc));
53 return;
54 }
55
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050056 auto syncfile = fs::path(SYNC_LIST_DIR_PATH) / SYNC_LIST_FILE_NAME;
57 if (fs::exists(syncfile))
58 {
59 std::string line;
60 std::ifstream file(syncfile.c_str());
61 while (std::getline(file, line))
62 {
Adriana Kobylak3a19e622018-11-09 11:39:46 -060063 addInotifyWatch(line);
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050064 }
65 }
66}
67
68SyncWatch::~SyncWatch()
69{
Brad Bishop02516d32018-11-19 15:29:26 -050070 if (inotifyFd != -1)
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050071 {
Brad Bishop02516d32018-11-19 15:29:26 -050072 close(inotifyFd);
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050073 }
74}
75
Adriana Kobylak292159f2020-05-05 09:25:55 -050076int SyncWatch::callback(sd_event_source* /* s */, int fd, uint32_t revents,
Adriana Kobylakb072d1b2018-04-24 11:37:21 -050077 void* userdata)
78{
Adriana Kobylaka9074342018-05-08 11:52:44 -050079 if (!(revents & EPOLLIN))
80 {
81 return 0;
82 }
83
84 constexpr auto maxBytes = 1024;
85 uint8_t buffer[maxBytes];
86 auto bytes = read(fd, buffer, maxBytes);
87 if (0 > bytes)
88 {
89 return 0;
90 }
91
92 auto syncWatch = static_cast<SyncWatch*>(userdata);
93 auto offset = 0;
94 while (offset < bytes)
95 {
96 auto event = reinterpret_cast<inotify_event*>(&buffer[offset]);
97
Brad Bishop02516d32018-11-19 15:29:26 -050098 // Watch was removed, re-add it if file still exists.
99 if (event->mask & IN_IGNORED)
Adriana Kobylaka9074342018-05-08 11:52:44 -0500100 {
Brad Bishop02516d32018-11-19 15:29:26 -0500101 if (fs::exists(syncWatch->fileMap[event->wd]))
Adriana Kobylak3a19e622018-11-09 11:39:46 -0600102 {
Brad Bishop02516d32018-11-19 15:29:26 -0500103 syncWatch->addInotifyWatch(syncWatch->fileMap[event->wd]);
Adriana Kobylak3a19e622018-11-09 11:39:46 -0600104 }
Brad Bishop02516d32018-11-19 15:29:26 -0500105 else
Adriana Kobylaka9074342018-05-08 11:52:44 -0500106 {
Brad Bishop02516d32018-11-19 15:29:26 -0500107 log<level::INFO>(
108 "The inotify watch was removed",
109 entry("FILENAME=%s",
110 (syncWatch->fileMap[event->wd]).c_str()));
Adriana Kobylaka9074342018-05-08 11:52:44 -0500111 }
Brad Bishop02516d32018-11-19 15:29:26 -0500112 return 0;
113 }
114
115 // fileMap<wd, path>
116 auto rc =
117 syncWatch->syncCallback(event->mask, syncWatch->fileMap[event->wd]);
118 if (rc)
119 {
120 return rc;
Adriana Kobylaka9074342018-05-08 11:52:44 -0500121 }
122
123 offset += offsetof(inotify_event, name) + event->len;
124 }
125
Adriana Kobylakb072d1b2018-04-24 11:37:21 -0500126 return 0;
127}
128
129} // namespace manager
130} // namespace software
131} // namespace phosphor