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