Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "watch.hpp" |
| 4 | |
| 5 | #include "image_manager.hpp" |
| 6 | |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 7 | #include <sys/inotify.h> |
| 8 | #include <unistd.h> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 9 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 10 | #include <phosphor-logging/lg2.hpp> |
Adriana Kobylak | 58aa750 | 2020-06-08 11:12:11 -0500 | [diff] [blame] | 11 | |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 12 | #include <cstddef> |
| 13 | #include <cstring> |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 14 | #include <filesystem> |
Gunnar Mills | b0ce996 | 2018-09-07 13:39:10 -0500 | [diff] [blame] | 15 | #include <stdexcept> |
| 16 | #include <string> |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 17 | #include <system_error> |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 18 | |
| 19 | namespace phosphor |
| 20 | { |
| 21 | namespace software |
| 22 | { |
| 23 | namespace manager |
| 24 | { |
| 25 | |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 26 | PHOSPHOR_LOG2_USING; |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 27 | using namespace std::string_literals; |
Adriana Kobylak | c98d912 | 2020-05-05 10:36:01 -0500 | [diff] [blame] | 28 | namespace fs = std::filesystem; |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 29 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 30 | Watch::Watch(sd_event* loop, std::function<int(std::string&)> imageCallback) : |
| 31 | imageCallback(imageCallback) |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 32 | { |
Gunnar Mills | 4e48fd5 | 2017-04-28 09:53:02 -0500 | [diff] [blame] | 33 | // Check if IMAGE DIR exists. |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 34 | std::error_code ec; |
Gunnar Mills | 4e48fd5 | 2017-04-28 09:53:02 -0500 | [diff] [blame] | 35 | fs::path imgDirPath(IMG_UPLOAD_DIR); |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 36 | if (!fs::is_directory(imgDirPath, ec)) |
Gunnar Mills | 4e48fd5 | 2017-04-28 09:53:02 -0500 | [diff] [blame] | 37 | { |
George Liu | 44b9fef | 2023-02-07 14:31:32 +0800 | [diff] [blame] | 38 | fs::create_directories(imgDirPath, ec); |
Gunnar Mills | 4e48fd5 | 2017-04-28 09:53:02 -0500 | [diff] [blame] | 39 | } |
| 40 | |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 41 | fd = inotify_init1(IN_NONBLOCK); |
| 42 | if (-1 == fd) |
| 43 | { |
| 44 | // Store a copy of errno, because the string creation below will |
| 45 | // invalidate errno due to one more system calls. |
| 46 | auto error = errno; |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 47 | throw std::runtime_error("inotify_init1 failed, errno="s + |
| 48 | std::strerror(error)); |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Gunnar Mills | 368cfcb | 2017-04-27 16:13:17 -0500 | [diff] [blame] | 51 | wd = inotify_add_watch(fd, IMG_UPLOAD_DIR, IN_CLOSE_WRITE); |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 52 | if (-1 == wd) |
| 53 | { |
| 54 | auto error = errno; |
| 55 | close(fd); |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 56 | throw std::runtime_error("inotify_add_watch failed, errno="s + |
| 57 | std::strerror(error)); |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 60 | auto rc = sd_event_add_io(loop, nullptr, fd, EPOLLIN, callback, this); |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 61 | if (0 > rc) |
| 62 | { |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 63 | throw std::runtime_error("failed to add to event loop, rc="s + |
| 64 | std::strerror(-rc)); |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | Watch::~Watch() |
| 69 | { |
Vernon Mauery | 8ff0ee5 | 2018-05-10 16:19:16 -0700 | [diff] [blame] | 70 | if (-1 != fd) |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 71 | { |
Vernon Mauery | 8ff0ee5 | 2018-05-10 16:19:16 -0700 | [diff] [blame] | 72 | if (-1 != wd) |
| 73 | { |
| 74 | inotify_rm_watch(fd, wd); |
| 75 | } |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 76 | close(fd); |
| 77 | } |
| 78 | } |
| 79 | |
Adriana Kobylak | 292159f | 2020-05-05 09:25:55 -0500 | [diff] [blame] | 80 | int Watch::callback(sd_event_source* /* s */, int fd, uint32_t revents, |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 81 | void* userdata) |
| 82 | { |
| 83 | if (!(revents & EPOLLIN)) |
| 84 | { |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | constexpr auto maxBytes = 1024; |
| 89 | uint8_t buffer[maxBytes]; |
| 90 | auto bytes = read(fd, buffer, maxBytes); |
| 91 | if (0 > bytes) |
| 92 | { |
| 93 | auto error = errno; |
Adriana Kobylak | 2285fe0 | 2018-02-27 15:36:59 -0600 | [diff] [blame] | 94 | throw std::runtime_error("failed to read inotify event, errno="s + |
| 95 | std::strerror(error)); |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | auto offset = 0; |
| 99 | while (offset < bytes) |
| 100 | { |
| 101 | auto event = reinterpret_cast<inotify_event*>(&buffer[offset]); |
Gunnar Mills | 368cfcb | 2017-04-27 16:13:17 -0500 | [diff] [blame] | 102 | if ((event->mask & IN_CLOSE_WRITE) && !(event->mask & IN_ISDIR)) |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 103 | { |
Gunnar Mills | 3027bba | 2017-04-27 15:49:03 -0500 | [diff] [blame] | 104 | auto tarballPath = std::string{IMG_UPLOAD_DIR} + '/' + event->name; |
| 105 | auto rc = static_cast<Watch*>(userdata)->imageCallback(tarballPath); |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 106 | if (rc < 0) |
| 107 | { |
Patrick Williams | c9bb642 | 2021-08-27 06:18:35 -0500 | [diff] [blame] | 108 | error("Error ({RC}) processing image {IMAGE}", "RC", rc, |
| 109 | "IMAGE", tarballPath); |
Gunnar Mills | e91d321 | 2017-04-19 15:42:47 -0500 | [diff] [blame] | 110 | } |
Deepak Kodihalli | 059e233 | 2017-04-12 06:40:53 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | offset += offsetof(inotify_event, name) + event->len; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | } // namespace manager |
| 120 | } // namespace software |
| 121 | } // namespace phosphor |