Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "sync_watch.hpp" |
| 4 | |
| 5 | #include <sys/inotify.h> |
| 6 | #include <unistd.h> |
| 7 | |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame^] | 8 | #include <filesystem> |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 9 | #include <fstream> |
| 10 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace software |
| 15 | { |
| 16 | namespace manager |
| 17 | { |
| 18 | |
| 19 | using namespace phosphor::logging; |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 20 | |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 21 | void SyncWatch::addInotifyWatch(const fs::path& path) |
| 22 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 23 | auto wd = |
| 24 | inotify_add_watch(inotifyFd, path.c_str(), IN_CLOSE_WRITE | IN_DELETE); |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 25 | if (-1 == wd) |
| 26 | { |
| 27 | log<level::ERR>("inotify_add_watch failed", entry("ERRNO=%d", errno), |
| 28 | entry("FILENAME=%s", path.c_str())); |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 29 | return; |
| 30 | } |
| 31 | |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 32 | fileMap[wd] = fs::path(path); |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 33 | } |
| 34 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 35 | SyncWatch::SyncWatch(sd_event& loop, |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 36 | std::function<int(int, fs::path&)> syncCallback) : |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 37 | inotifyFd(-1), |
| 38 | syncCallback(syncCallback), loop(loop) |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 39 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 40 | auto fd = inotify_init1(IN_NONBLOCK); |
| 41 | if (-1 == fd) |
| 42 | { |
| 43 | log<level::ERR>("inotify_init1 failed", entry("ERRNO=%d", errno)); |
| 44 | return; |
| 45 | } |
| 46 | inotifyFd = fd; |
| 47 | |
| 48 | auto rc = sd_event_add_io(&loop, nullptr, fd, EPOLLIN, callback, this); |
| 49 | if (0 > rc) |
| 50 | { |
| 51 | log<level::ERR>("failed to add to event loop", entry("RC=%d", rc)); |
| 52 | return; |
| 53 | } |
| 54 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 55 | auto syncfile = fs::path(SYNC_LIST_DIR_PATH) / SYNC_LIST_FILE_NAME; |
| 56 | if (fs::exists(syncfile)) |
| 57 | { |
| 58 | std::string line; |
| 59 | std::ifstream file(syncfile.c_str()); |
| 60 | while (std::getline(file, line)) |
| 61 | { |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 62 | addInotifyWatch(line); |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | SyncWatch::~SyncWatch() |
| 68 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 69 | if (inotifyFd != -1) |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 70 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 71 | close(inotifyFd); |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
Adriana Kobylak | 292159f | 2020-05-05 09:25:55 -0500 | [diff] [blame] | 75 | int SyncWatch::callback(sd_event_source* /* s */, int fd, uint32_t revents, |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 76 | void* userdata) |
| 77 | { |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 78 | if (!(revents & EPOLLIN)) |
| 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | constexpr auto maxBytes = 1024; |
| 84 | uint8_t buffer[maxBytes]; |
| 85 | auto bytes = read(fd, buffer, maxBytes); |
| 86 | if (0 > bytes) |
| 87 | { |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | auto syncWatch = static_cast<SyncWatch*>(userdata); |
| 92 | auto offset = 0; |
| 93 | while (offset < bytes) |
| 94 | { |
| 95 | auto event = reinterpret_cast<inotify_event*>(&buffer[offset]); |
| 96 | |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 97 | // Watch was removed, re-add it if file still exists. |
| 98 | if (event->mask & IN_IGNORED) |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 99 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 100 | if (fs::exists(syncWatch->fileMap[event->wd])) |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 101 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 102 | syncWatch->addInotifyWatch(syncWatch->fileMap[event->wd]); |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 103 | } |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 104 | else |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 105 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 106 | log<level::INFO>( |
| 107 | "The inotify watch was removed", |
| 108 | entry("FILENAME=%s", |
| 109 | (syncWatch->fileMap[event->wd]).c_str())); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 110 | } |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | // fileMap<wd, path> |
| 115 | auto rc = |
| 116 | syncWatch->syncCallback(event->mask, syncWatch->fileMap[event->wd]); |
| 117 | if (rc) |
| 118 | { |
| 119 | return rc; |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | offset += offsetof(inotify_event, name) + event->len; |
| 123 | } |
| 124 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | } // namespace manager |
| 129 | } // namespace software |
| 130 | } // namespace phosphor |