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