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