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