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