Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 1 | #include "config.h" |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 2 | |
| 3 | #include "dump_manager.hpp" |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 4 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 5 | #include "dump_internal.hpp" |
| 6 | #include "xyz/openbmc_project/Common/error.hpp" |
Jayanth Othayoth | ab7f920 | 2017-08-02 06:24:58 -0500 | [diff] [blame] | 7 | #include "xyz/openbmc_project/Dump/Create/error.hpp" |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 8 | |
| 9 | #include <sys/inotify.h> |
| 10 | #include <unistd.h> |
| 11 | |
| 12 | #include <phosphor-logging/elog-errors.hpp> |
| 13 | #include <phosphor-logging/elog.hpp> |
| 14 | #include <regex> |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 15 | |
| 16 | namespace phosphor |
| 17 | { |
| 18 | namespace dump |
| 19 | { |
| 20 | |
| 21 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 22 | using namespace phosphor::logging; |
| 23 | |
| 24 | namespace internal |
| 25 | { |
| 26 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 27 | void Manager::create(Type type, std::vector<std::string> fullPaths) |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 28 | { |
Jayanth Othayoth | d3273ea | 2017-07-12 22:55:32 -0500 | [diff] [blame] | 29 | dumpMgr.phosphor::dump::Manager::captureDump(type, fullPaths); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 30 | } |
| 31 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 32 | } // namespace internal |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 33 | |
| 34 | uint32_t Manager::createDump() |
| 35 | { |
| 36 | std::vector<std::string> paths; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 37 | return captureDump(Type::UserRequested, paths); |
| 38 | } |
| 39 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 40 | uint32_t Manager::captureDump(Type type, |
| 41 | const std::vector<std::string>& fullPaths) |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 42 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 43 | // Get Dump size. |
Jayanth Othayoth | 9c15558 | 2017-08-09 07:47:45 -0500 | [diff] [blame] | 44 | auto size = getAllowedSize(); |
| 45 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 46 | pid_t pid = fork(); |
| 47 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 48 | if (pid == 0) |
| 49 | { |
Jayanth Othayoth | f9009a2 | 2017-07-12 19:40:34 -0500 | [diff] [blame] | 50 | fs::path dumpPath(BMC_DUMP_PATH); |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 51 | auto id = std::to_string(lastEntryId + 1); |
Jayanth Othayoth | 9c15558 | 2017-08-09 07:47:45 -0500 | [diff] [blame] | 52 | dumpPath /= id; |
Jayanth Othayoth | f9009a2 | 2017-07-12 19:40:34 -0500 | [diff] [blame] | 53 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 54 | // get dreport type map entry |
Marri Devender Rao | 0deb287 | 2018-11-12 07:45:54 -0600 | [diff] [blame^] | 55 | auto tempType = TypeMap.find(type); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 56 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 57 | execl("/usr/bin/dreport", "dreport", "-d", dumpPath.c_str(), "-i", |
| 58 | id.c_str(), "-s", std::to_string(size).c_str(), "-q", "-v", "-p", |
| 59 | fullPaths.empty() ? "" : fullPaths.front().c_str(), "-t", |
| 60 | tempType->second.c_str(), nullptr); |
Jayanth Othayoth | 9c15558 | 2017-08-09 07:47:45 -0500 | [diff] [blame] | 61 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 62 | // dreport script execution is failed. |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 63 | auto error = errno; |
Jayanth Othayoth | 9c15558 | 2017-08-09 07:47:45 -0500 | [diff] [blame] | 64 | log<level::ERR>("Error occurred during dreport function execution", |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 65 | entry("ERRNO=%d", error)); |
| 66 | elog<InternalFailure>(); |
| 67 | } |
| 68 | else if (pid > 0) |
| 69 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 70 | auto rc = sd_event_add_child(eventLoop.get(), nullptr, pid, |
| 71 | WEXITED | WSTOPPED, callback, nullptr); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 72 | if (0 > rc) |
| 73 | { |
| 74 | // Failed to add to event loop |
| 75 | log<level::ERR>("Error occurred during the sd_event_add_child call", |
Gunnar Mills | 11eaab7 | 2017-10-19 16:07:31 -0500 | [diff] [blame] | 76 | entry("RC=%d", rc)); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 77 | elog<InternalFailure>(); |
| 78 | } |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | auto error = errno; |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 83 | log<level::ERR>("Error occurred during fork", entry("ERRNO=%d", error)); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 84 | elog<InternalFailure>(); |
| 85 | } |
| 86 | |
| 87 | return ++lastEntryId; |
| 88 | } |
| 89 | |
| 90 | void Manager::createEntry(const fs::path& file) |
| 91 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 92 | // Dump File Name format obmcdump_ID_EPOCHTIME.EXT |
| 93 | static constexpr auto ID_POS = 1; |
| 94 | static constexpr auto EPOCHTIME_POS = 2; |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 95 | std::regex file_regex("obmcdump_([0-9]+)_([0-9]+).([a-zA-Z0-9]+)"); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 96 | |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 97 | std::smatch match; |
| 98 | std::string name = file.filename(); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 99 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 100 | if (!((std::regex_search(name, match, file_regex)) && (match.size() > 0))) |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 101 | { |
| 102 | log<level::ERR>("Invalid Dump file name", |
Joseph Reynolds | af48762 | 2018-05-10 15:54:16 -0500 | [diff] [blame] | 103 | entry("FILENAME=%s", file.filename().c_str())); |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 104 | return; |
| 105 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 106 | |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 107 | auto idString = match[ID_POS]; |
| 108 | auto msString = match[EPOCHTIME_POS]; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 109 | |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 110 | try |
| 111 | { |
| 112 | auto id = stoul(idString); |
| 113 | // Entry Object path. |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 114 | auto objPath = fs::path(OBJ_ENTRY) / std::to_string(id); |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 115 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 116 | entries.insert(std::make_pair( |
| 117 | id, |
| 118 | std::make_unique<Entry>(bus, objPath.c_str(), id, stoull(msString), |
| 119 | fs::file_size(file), file, *this))); |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 120 | } |
| 121 | catch (const std::invalid_argument& e) |
| 122 | { |
| 123 | log<level::ERR>(e.what()); |
| 124 | return; |
| 125 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void Manager::erase(uint32_t entryId) |
| 129 | { |
| 130 | entries.erase(entryId); |
| 131 | } |
| 132 | |
Nagaraju Goruganti | 3c899a4 | 2017-09-12 06:14:46 -0500 | [diff] [blame] | 133 | void Manager::deleteAll() |
| 134 | { |
| 135 | auto iter = entries.begin(); |
| 136 | while (iter != entries.end()) |
| 137 | { |
| 138 | auto& entry = iter->second; |
| 139 | entry->delete_(); |
| 140 | ++iter; |
| 141 | } |
| 142 | } |
| 143 | |
Jayanth Othayoth | bcb174b | 2017-07-02 06:29:24 -0500 | [diff] [blame] | 144 | void Manager::watchCallback(const UserMap& fileInfo) |
| 145 | { |
| 146 | for (const auto& i : fileInfo) |
| 147 | { |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 148 | // For any new dump file create dump entry object |
| 149 | // and associated inotify watch. |
Jayanth Othayoth | bcb174b | 2017-07-02 06:29:24 -0500 | [diff] [blame] | 150 | if (IN_CLOSE_WRITE == i.second) |
| 151 | { |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 152 | removeWatch(i.first); |
| 153 | |
Jayanth Othayoth | bcb174b | 2017-07-02 06:29:24 -0500 | [diff] [blame] | 154 | createEntry(i.first); |
| 155 | } |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 156 | // Start inotify watch on newly created directory. |
| 157 | else if ((IN_CREATE == i.second) && fs::is_directory(i.first)) |
| 158 | { |
| 159 | auto watchObj = std::make_unique<Watch>( |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 160 | eventLoop, IN_NONBLOCK, IN_CLOSE_WRITE, EPOLLIN, i.first, |
| 161 | std::bind(std::mem_fn(&phosphor::dump::Manager::watchCallback), |
| 162 | this, std::placeholders::_1)); |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 163 | |
Jayanth Othayoth | ab7f920 | 2017-08-02 06:24:58 -0500 | [diff] [blame] | 164 | childWatchMap.emplace(i.first, std::move(watchObj)); |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 165 | } |
Jayanth Othayoth | bcb174b | 2017-07-02 06:29:24 -0500 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 169 | void Manager::removeWatch(const fs::path& path) |
| 170 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 171 | // Delete Watch entry from map. |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 172 | childWatchMap.erase(path); |
| 173 | } |
| 174 | |
Jayanth Othayoth | 4309659 | 2017-07-20 02:17:37 -0500 | [diff] [blame] | 175 | void Manager::restore() |
| 176 | { |
| 177 | fs::path dir(BMC_DUMP_PATH); |
| 178 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 179 | { |
| 180 | return; |
| 181 | } |
| 182 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 183 | // Dump file path: <BMC_DUMP_PATH>/<id>/<filename> |
Jayanth Othayoth | 4309659 | 2017-07-20 02:17:37 -0500 | [diff] [blame] | 184 | for (const auto& p : fs::directory_iterator(dir)) |
| 185 | { |
| 186 | auto idStr = p.path().filename().string(); |
| 187 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 188 | // Consider only directory's with dump id as name. |
| 189 | // Note: As per design one file per directory. |
Jayanth Othayoth | 4309659 | 2017-07-20 02:17:37 -0500 | [diff] [blame] | 190 | if ((fs::is_directory(p.path())) && |
| 191 | std::all_of(idStr.begin(), idStr.end(), ::isdigit)) |
| 192 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 193 | lastEntryId = |
| 194 | std::max(lastEntryId, static_cast<uint32_t>(std::stoul(idStr))); |
Jayanth Othayoth | 4309659 | 2017-07-20 02:17:37 -0500 | [diff] [blame] | 195 | auto fileIt = fs::directory_iterator(p.path()); |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 196 | // Create dump entry d-bus object. |
Jayanth Othayoth | 4309659 | 2017-07-20 02:17:37 -0500 | [diff] [blame] | 197 | if (fileIt != fs::end(fileIt)) |
| 198 | { |
| 199 | createEntry(fileIt->path()); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Jayanth Othayoth | ab7f920 | 2017-08-02 06:24:58 -0500 | [diff] [blame] | 205 | size_t Manager::getAllowedSize() |
| 206 | { |
| 207 | using namespace sdbusplus::xyz::openbmc_project::Dump::Create::Error; |
| 208 | using Reason = xyz::openbmc_project::Dump::Create::QuotaExceeded::REASON; |
| 209 | |
| 210 | auto size = 0; |
| 211 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 212 | // Get current size of the dump directory. |
Jayanth Othayoth | 104f57c | 2017-08-09 06:19:32 -0500 | [diff] [blame] | 213 | for (const auto& p : fs::recursive_directory_iterator(BMC_DUMP_PATH)) |
Jayanth Othayoth | ab7f920 | 2017-08-02 06:24:58 -0500 | [diff] [blame] | 214 | { |
Jayanth Othayoth | 104f57c | 2017-08-09 06:19:32 -0500 | [diff] [blame] | 215 | if (!fs::is_directory(p)) |
| 216 | { |
| 217 | size += fs::file_size(p); |
| 218 | } |
Jayanth Othayoth | ab7f920 | 2017-08-02 06:24:58 -0500 | [diff] [blame] | 219 | } |
Jayanth Othayoth | 104f57c | 2017-08-09 06:19:32 -0500 | [diff] [blame] | 220 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 221 | // Convert size into KB |
Jayanth Othayoth | 104f57c | 2017-08-09 06:19:32 -0500 | [diff] [blame] | 222 | size = size / 1024; |
| 223 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 224 | // Set the Dump size to Maximum if the free space is greater than |
| 225 | // Dump max size otherwise return the available size. |
Jayanth Othayoth | 104f57c | 2017-08-09 06:19:32 -0500 | [diff] [blame] | 226 | |
| 227 | size = (size > BMC_DUMP_TOTAL_SIZE ? 0 : BMC_DUMP_TOTAL_SIZE - size); |
| 228 | |
| 229 | if (size < BMC_DUMP_MIN_SPACE_REQD) |
Jayanth Othayoth | ab7f920 | 2017-08-02 06:24:58 -0500 | [diff] [blame] | 230 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 231 | // Reached to maximum limit |
Jayanth Othayoth | ab7f920 | 2017-08-02 06:24:58 -0500 | [diff] [blame] | 232 | elog<QuotaExceeded>(Reason("Not enough space: Delete old dumps")); |
| 233 | } |
Jayanth Othayoth | 104f57c | 2017-08-09 06:19:32 -0500 | [diff] [blame] | 234 | if (size > BMC_DUMP_MAX_SIZE) |
| 235 | { |
| 236 | size = BMC_DUMP_MAX_SIZE; |
| 237 | } |
| 238 | |
Jayanth Othayoth | ab7f920 | 2017-08-02 06:24:58 -0500 | [diff] [blame] | 239 | return size; |
| 240 | } |
| 241 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 242 | } // namespace dump |
| 243 | } // namespace phosphor |