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