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