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 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/lg2.hpp> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 9 | |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 10 | #include <filesystem> |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 11 | #include <fstream> |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace software |
| 16 | { |
| 17 | namespace manager |
| 18 | { |
| 19 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 20 | PHOSPHOR_LOG2_USING; |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 21 | |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 22 | void SyncWatch::addInotifyWatch(const fs::path& path) |
| 23 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 24 | auto wd = |
| 25 | inotify_add_watch(inotifyFd, path.c_str(), IN_CLOSE_WRITE | IN_DELETE); |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 26 | if (-1 == wd) |
| 27 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 28 | error("inotify_add_watch on {PATH} failed: {ERRNO}", "ERRNO", errno, |
| 29 | "PATH", path); |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 30 | return; |
| 31 | } |
| 32 | |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 33 | fileMap[wd] = fs::path(path); |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 34 | } |
| 35 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 36 | SyncWatch::SyncWatch(sd_event& loop, |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 37 | std::function<int(int, fs::path&)> syncCallback) : |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 38 | inotifyFd(-1), |
| 39 | syncCallback(syncCallback), loop(loop) |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 40 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 41 | auto fd = inotify_init1(IN_NONBLOCK); |
| 42 | if (-1 == fd) |
| 43 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 44 | error("inotify_init1 failed: {ERRNO}", "ERRNO", errno); |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 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 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 52 | error("failed to add to event loop: {RC}", "RC", rc); |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 56 | 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 Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 63 | addInotifyWatch(line); |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | SyncWatch::~SyncWatch() |
| 69 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 70 | if (inotifyFd != -1) |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 71 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 72 | close(inotifyFd); |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Adriana Kobylak | 292159f | 2020-05-05 09:25:55 -0500 | [diff] [blame] | 76 | int SyncWatch::callback(sd_event_source* /* s */, int fd, uint32_t revents, |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 77 | void* userdata) |
| 78 | { |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 79 | 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 Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 98 | // Watch was removed, re-add it if file still exists. |
| 99 | if (event->mask & IN_IGNORED) |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 100 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 101 | if (fs::exists(syncWatch->fileMap[event->wd])) |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 102 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 103 | syncWatch->addInotifyWatch(syncWatch->fileMap[event->wd]); |
Adriana Kobylak | 3a19e62 | 2018-11-09 11:39:46 -0600 | [diff] [blame] | 104 | } |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 105 | else |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 106 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 107 | info("The inotify watch on {PATH} was removed", "PATH", |
| 108 | syncWatch->fileMap[event->wd]); |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 109 | } |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | // fileMap<wd, path> |
| 114 | auto rc = |
| 115 | syncWatch->syncCallback(event->mask, syncWatch->fileMap[event->wd]); |
| 116 | if (rc) |
| 117 | { |
| 118 | return rc; |
Adriana Kobylak | a907434 | 2018-05-08 11:52:44 -0500 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | offset += offsetof(inotify_event, name) + event->len; |
| 122 | } |
| 123 | |
Adriana Kobylak | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | } // namespace manager |
| 128 | } // namespace software |
| 129 | } // namespace phosphor |