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