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