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 | b072d1b | 2018-04-24 11:37:21 -0500 | [diff] [blame] | 8 | #include <experimental/filesystem> |
| 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; |
| 20 | namespace fs = std::experimental::filesystem; |
| 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 | { |
| 28 | log<level::ERR>("inotify_add_watch failed", entry("ERRNO=%d", errno), |
| 29 | entry("FILENAME=%s", path.c_str())); |
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 | { |
| 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 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 | |
| 76 | int SyncWatch::callback(sd_event_source* s, int fd, uint32_t revents, |
| 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 | { |
Brad Bishop | 02516d3 | 2018-11-19 15:29:26 -0500 | [diff] [blame] | 107 | log<level::INFO>( |
| 108 | "The inotify watch was removed", |
| 109 | entry("FILENAME=%s", |
| 110 | (syncWatch->fileMap[event->wd]).c_str())); |
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> |
| 116 | auto rc = |
| 117 | syncWatch->syncCallback(event->mask, syncWatch->fileMap[event->wd]); |
| 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 |