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 | |
| 5 | #include <phosphor-logging/elog-errors.hpp> |
| 6 | |
| 7 | #include "dump_manager.hpp" |
| 8 | #include "dump_internal.hpp" |
| 9 | #include "xyz/openbmc_project/Common/error.hpp" |
| 10 | #include "config.h" |
| 11 | |
| 12 | namespace phosphor |
| 13 | { |
| 14 | namespace dump |
| 15 | { |
| 16 | |
| 17 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 18 | using namespace phosphor::logging; |
| 19 | |
| 20 | namespace internal |
| 21 | { |
| 22 | |
| 23 | void Manager::create( |
| 24 | Type type, |
| 25 | std::vector<std::string> fullPaths) |
| 26 | { |
Jayanth Othayoth | d3273ea | 2017-07-12 22:55:32 -0500 | [diff] [blame] | 27 | dumpMgr.phosphor::dump::Manager::captureDump(type, fullPaths); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | } //namepsace internal |
| 31 | |
| 32 | uint32_t Manager::createDump() |
| 33 | { |
| 34 | std::vector<std::string> paths; |
| 35 | |
| 36 | return captureDump(Type::UserRequested, paths); |
| 37 | } |
| 38 | |
| 39 | uint32_t Manager::captureDump( |
| 40 | Type type, |
| 41 | const std::vector<std::string>& fullPaths) |
| 42 | { |
| 43 | pid_t pid = fork(); |
| 44 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 45 | if (pid == 0) |
| 46 | { |
Jayanth Othayoth | f9009a2 | 2017-07-12 19:40:34 -0500 | [diff] [blame] | 47 | fs::path dumpPath(BMC_DUMP_PATH); |
| 48 | |
| 49 | dumpPath /= std::to_string(lastEntryId + 1); |
Jayanth Othayoth | f655cb4 | 2017-07-24 02:05:38 -0500 | [diff] [blame^] | 50 | execl("/usr/bin/ffdc", "ffdc", "-d", dumpPath.c_str(), "-e", nullptr); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 51 | |
| 52 | //ffdc script execution is failed. |
| 53 | auto error = errno; |
| 54 | log<level::ERR>("Error occurred during ffdc function execution", |
| 55 | entry("ERRNO=%d", error)); |
| 56 | elog<InternalFailure>(); |
| 57 | } |
| 58 | else if (pid > 0) |
| 59 | { |
| 60 | auto rc = sd_event_add_child(eventLoop.get(), |
| 61 | nullptr, |
| 62 | pid, |
| 63 | WEXITED | WSTOPPED, |
| 64 | callback, |
| 65 | nullptr); |
| 66 | if (0 > rc) |
| 67 | { |
| 68 | // Failed to add to event loop |
| 69 | log<level::ERR>("Error occurred during the sd_event_add_child call", |
| 70 | entry("rc=%d", rc)); |
| 71 | elog<InternalFailure>(); |
| 72 | } |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | auto error = errno; |
| 77 | log<level::ERR>("Error occurred during fork", |
| 78 | entry("ERRNO=%d", error)); |
| 79 | elog<InternalFailure>(); |
| 80 | } |
| 81 | |
| 82 | return ++lastEntryId; |
| 83 | } |
| 84 | |
| 85 | void Manager::createEntry(const fs::path& file) |
| 86 | { |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 87 | //Dump File Name format obmcdump_ID_EPOCHTIME.EXT |
| 88 | static constexpr auto ID_POS = 1; |
| 89 | static constexpr auto EPOCHTIME_POS = 2; |
| 90 | 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] | 91 | |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 92 | std::smatch match; |
| 93 | std::string name = file.filename(); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 94 | |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 95 | if (!((std::regex_search(name, match, file_regex)) && |
| 96 | (match.size() > 0))) |
| 97 | { |
| 98 | log<level::ERR>("Invalid Dump file name", |
| 99 | entry("Filename=%s", file.filename())); |
| 100 | return; |
| 101 | } |
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 | auto idString = match[ID_POS]; |
| 104 | auto msString = match[EPOCHTIME_POS]; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 105 | |
Jayanth Othayoth | 481bb3d | 2017-07-13 03:33:34 -0500 | [diff] [blame] | 106 | try |
| 107 | { |
| 108 | auto id = stoul(idString); |
| 109 | // Entry Object path. |
| 110 | auto objPath = fs::path(OBJ_ENTRY) / std::to_string(id); |
| 111 | |
| 112 | entries.insert(std::make_pair(id, |
| 113 | std::make_unique<Entry>( |
| 114 | bus, |
| 115 | objPath.c_str(), |
| 116 | id, |
| 117 | stoull(msString), |
| 118 | fs::file_size(file), |
| 119 | file, |
| 120 | *this))); |
| 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 | |
Jayanth Othayoth | bcb174b | 2017-07-02 06:29:24 -0500 | [diff] [blame] | 134 | void Manager::watchCallback(const UserMap& fileInfo) |
| 135 | { |
| 136 | for (const auto& i : fileInfo) |
| 137 | { |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 138 | // For any new dump file create dump entry object |
| 139 | // and associated inotify watch. |
Jayanth Othayoth | bcb174b | 2017-07-02 06:29:24 -0500 | [diff] [blame] | 140 | if (IN_CLOSE_WRITE == i.second) |
| 141 | { |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 142 | removeWatch(i.first); |
| 143 | |
Jayanth Othayoth | bcb174b | 2017-07-02 06:29:24 -0500 | [diff] [blame] | 144 | createEntry(i.first); |
| 145 | } |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 146 | // Start inotify watch on newly created directory. |
| 147 | else if ((IN_CREATE == i.second) && fs::is_directory(i.first)) |
| 148 | { |
| 149 | auto watchObj = std::make_unique<Watch>( |
| 150 | eventLoop, |
| 151 | IN_NONBLOCK, |
| 152 | IN_CLOSE_WRITE, |
| 153 | EPOLLIN, |
| 154 | i.first, |
| 155 | std::bind( |
| 156 | std::mem_fn( |
| 157 | &phosphor::dump::Manager::watchCallback), |
| 158 | this, std::placeholders::_1)); |
| 159 | |
| 160 | childWatchMap.emplace(i.first, std::move(watchObj)); |
| 161 | } |
| 162 | |
Jayanth Othayoth | bcb174b | 2017-07-02 06:29:24 -0500 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
Jayanth Othayoth | 764d1b2 | 2017-07-12 19:14:02 -0500 | [diff] [blame] | 166 | void Manager::removeWatch(const fs::path& path) |
| 167 | { |
| 168 | //Delete Watch entry from map. |
| 169 | childWatchMap.erase(path); |
| 170 | } |
| 171 | |
Jayanth Othayoth | 4309659 | 2017-07-20 02:17:37 -0500 | [diff] [blame] | 172 | void Manager::restore() |
| 173 | { |
| 174 | fs::path dir(BMC_DUMP_PATH); |
| 175 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 176 | { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | //Dump file path: <BMC_DUMP_PATH>/<id>/<filename> |
| 181 | for (const auto& p : fs::directory_iterator(dir)) |
| 182 | { |
| 183 | auto idStr = p.path().filename().string(); |
| 184 | |
| 185 | //Consider only directory's with dump id as name. |
| 186 | //Note: As per design one file per directory. |
| 187 | if ((fs::is_directory(p.path())) && |
| 188 | std::all_of(idStr.begin(), idStr.end(), ::isdigit)) |
| 189 | { |
| 190 | lastEntryId = std::max(lastEntryId, |
| 191 | static_cast<uint32_t>(std::stoul(idStr))); |
| 192 | auto fileIt = fs::directory_iterator(p.path()); |
| 193 | //Create dump entry d-bus object. |
| 194 | if (fileIt != fs::end(fileIt)) |
| 195 | { |
| 196 | createEntry(fileIt->path()); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 202 | } //namespace dump |
| 203 | } //namespace phosphor |