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