Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "log_manager.hpp" |
| 4 | |
| 5 | #include "elog_entry.hpp" |
| 6 | #include "elog_meta.hpp" |
| 7 | #include "elog_serialize.hpp" |
| 8 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 9 | #include <poll.h> |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 10 | #include <sys/inotify.h> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 11 | #include <systemd/sd-bus.h> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 12 | #include <systemd/sd-journal.h> |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 13 | #include <unistd.h> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 14 | |
| 15 | #include <chrono> |
| 16 | #include <cstdio> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 17 | #include <cstring> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 18 | #include <fstream> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 19 | #include <functional> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 20 | #include <future> |
| 21 | #include <iostream> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 22 | #include <map> |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 23 | #include <phosphor-logging/log.hpp> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 24 | #include <sdbusplus/vtable.hpp> |
| 25 | #include <set> |
| 26 | #include <string> |
| 27 | #include <vector> |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 28 | |
| 29 | using namespace phosphor::logging; |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 30 | using namespace std::chrono; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 31 | extern const std::map<metadata::Metadata, |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 32 | std::function<metadata::associations::Type>> |
| 33 | meta; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 34 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 35 | namespace phosphor |
| 36 | { |
| 37 | namespace logging |
| 38 | { |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 39 | namespace internal |
| 40 | { |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 41 | inline auto getLevel(const std::string& errMsg) |
| 42 | { |
| 43 | auto reqLevel = Entry::Level::Error; // Default to Error |
| 44 | |
| 45 | auto levelmap = g_errLevelMap.find(errMsg); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 46 | if (levelmap != g_errLevelMap.end()) |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 47 | { |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 48 | reqLevel = static_cast<Entry::Level>(levelmap->second); |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 49 | } |
| 50 | |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 51 | return reqLevel; |
| 52 | } |
| 53 | |
Nagaraju Goruganti | 477b731 | 2018-06-25 23:28:58 -0500 | [diff] [blame] | 54 | int Manager::getRealErrSize() |
| 55 | { |
| 56 | return realErrors.size(); |
| 57 | } |
| 58 | |
| 59 | int Manager::getInfoErrSize() |
| 60 | { |
| 61 | return infoErrors.size(); |
| 62 | } |
| 63 | |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 64 | void Manager::commit(uint64_t transactionId, std::string errMsg) |
| 65 | { |
| 66 | auto level = getLevel(errMsg); |
| 67 | _commit(transactionId, std::move(errMsg), level); |
| 68 | } |
| 69 | |
| 70 | void Manager::commitWithLvl(uint64_t transactionId, std::string errMsg, |
| 71 | uint32_t errLvl) |
| 72 | { |
| 73 | _commit(transactionId, std::move(errMsg), |
| 74 | static_cast<Entry::Level>(errLvl)); |
| 75 | } |
| 76 | |
| 77 | void Manager::_commit(uint64_t transactionId, std::string&& errMsg, |
| 78 | Entry::Level errLvl) |
| 79 | { |
| 80 | if (errLvl < Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 81 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 82 | if (realErrors.size() >= ERROR_CAP) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 83 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 84 | erase(realErrors.front()); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | if (infoErrors.size() >= ERROR_INFO_CAP) |
| 90 | { |
| 91 | erase(infoErrors.front()); |
| 92 | } |
| 93 | } |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 94 | constexpr const auto transactionIdVar = "TRANSACTION_ID"; |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 95 | // Length of 'TRANSACTION_ID' string. |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 96 | constexpr const auto transactionIdVarSize = std::strlen(transactionIdVar); |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 97 | // Length of 'TRANSACTION_ID=' string. |
| 98 | constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 99 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 100 | // Flush all the pending log messages into the journal |
| 101 | journalSync(); |
Adriana Kobylak | cfd9a7d | 2017-06-07 11:57:31 -0500 | [diff] [blame] | 102 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 103 | sd_journal* j = nullptr; |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 104 | int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 105 | if (rc < 0) |
| 106 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 107 | logging::log<logging::level::ERR>( |
| 108 | "Failed to open journal", |
| 109 | logging::entry("DESCRIPTION=%s", strerror(-rc))); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 110 | return; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 111 | } |
| 112 | |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 113 | std::string transactionIdStr = std::to_string(transactionId); |
Adriana Kobylak | d722b3a | 2017-02-28 12:10:44 -0600 | [diff] [blame] | 114 | std::set<std::string> metalist; |
| 115 | auto metamap = g_errMetaMap.find(errMsg); |
| 116 | if (metamap != g_errMetaMap.end()) |
| 117 | { |
| 118 | metalist.insert(metamap->second.begin(), metamap->second.end()); |
| 119 | } |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 120 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 121 | // Add _PID field information in AdditionalData. |
Jayanth Othayoth | db18ebe | 2017-09-04 00:48:02 -0500 | [diff] [blame] | 122 | metalist.insert("_PID"); |
| 123 | |
Tom Joseph | 7a33ee4 | 2017-07-25 00:04:20 +0530 | [diff] [blame] | 124 | std::vector<std::string> additionalData; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 125 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 126 | // Read the journal from the end to get the most recent entry first. |
| 127 | // The result from the sd_journal_get_data() is of the form VARIABLE=value. |
| 128 | SD_JOURNAL_FOREACH_BACKWARDS(j) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 129 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 130 | const char* data = nullptr; |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 131 | size_t length = 0; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 132 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 133 | // Look for the transaction id metadata variable |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 134 | rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data, |
| 135 | &length); |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 136 | if (rc < 0) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 137 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 138 | // This journal entry does not have the TRANSACTION_ID |
| 139 | // metadata variable. |
| 140 | continue; |
| 141 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 142 | |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 143 | // journald does not guarantee that sd_journal_get_data() returns NULL |
| 144 | // terminated strings, so need to specify the size to use to compare, |
| 145 | // use the returned length instead of anything that relies on NULL |
| 146 | // terminators like strlen(). |
| 147 | // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove |
| 148 | // the TRANSACTION_ID characters plus the (=) sign to do the comparison. |
| 149 | // 'data + transactionIdVarOffset' will be in the form of '1234'. |
| 150 | // 'length - transactionIdVarOffset' will be the length of '1234'. |
| 151 | if ((length <= (transactionIdVarOffset)) || |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 152 | (transactionIdStr.compare(0, transactionIdStr.size(), |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 153 | data + transactionIdVarOffset, |
| 154 | length - transactionIdVarOffset) != 0)) |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 155 | { |
| 156 | // The value of the TRANSACTION_ID metadata is not the requested |
| 157 | // transaction id number. |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | // Search for all metadata variables in the current journal entry. |
| 162 | for (auto i = metalist.cbegin(); i != metalist.cend();) |
| 163 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 164 | rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data, |
| 165 | &length); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 166 | if (rc < 0) |
| 167 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 168 | // Metadata variable not found, check next metadata variable. |
| 169 | i++; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 170 | continue; |
| 171 | } |
| 172 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 173 | // Metadata variable found, save it and remove it from the set. |
| 174 | additionalData.emplace_back(data, length); |
| 175 | i = metalist.erase(i); |
| 176 | } |
| 177 | if (metalist.empty()) |
| 178 | { |
| 179 | // All metadata variables found, break out of journal loop. |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 180 | break; |
| 181 | } |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 182 | } |
| 183 | if (!metalist.empty()) |
| 184 | { |
| 185 | // Not all the metadata variables were found in the journal. |
| 186 | for (auto& metaVarStr : metalist) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 187 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 188 | logging::log<logging::level::INFO>( |
| 189 | "Failed to find metadata", |
| 190 | logging::entry("META_FIELD=%s", metaVarStr.c_str())); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 194 | sd_journal_close(j); |
| 195 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 196 | // Create error Entry dbus object |
| 197 | entryId++; |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 198 | if (errLvl >= Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 199 | { |
| 200 | infoErrors.push_back(entryId); |
| 201 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 202 | else |
| 203 | { |
| 204 | realErrors.push_back(entryId); |
| 205 | } |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 206 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 207 | std::chrono::system_clock::now().time_since_epoch()) |
| 208 | .count(); |
| 209 | auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 210 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 211 | AssociationList objects{}; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 212 | processMetadata(errMsg, additionalData, objects); |
| 213 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 214 | auto e = std::make_unique<Entry>(busLog, objPath, entryId, |
| 215 | ms, // Milliseconds since 1970 |
| 216 | errLvl, std::move(errMsg), |
| 217 | std::move(additionalData), |
| 218 | std::move(objects), fwVersion, *this); |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 219 | serialize(*e); |
| 220 | entries.insert(std::make_pair(entryId, std::move(e))); |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 223 | void Manager::processMetadata(const std::string& errorName, |
| 224 | const std::vector<std::string>& additionalData, |
| 225 | AssociationList& objects) const |
| 226 | { |
| 227 | // additionalData is a list of "metadata=value" |
| 228 | constexpr auto separator = '='; |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 229 | for (const auto& entryItem : additionalData) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 230 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 231 | auto found = entryItem.find(separator); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 232 | if (std::string::npos != found) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 233 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 234 | auto metadata = entryItem.substr(0, found); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 235 | auto iter = meta.find(metadata); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 236 | if (meta.end() != iter) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 237 | { |
| 238 | (iter->second)(metadata, additionalData, objects); |
| 239 | } |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 244 | void Manager::erase(uint32_t entryId) |
| 245 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 246 | auto entryFound = entries.find(entryId); |
| 247 | if (entries.end() != entryFound) |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 248 | { |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 249 | // Delete the persistent representation of this error. |
| 250 | fs::path errorPath(ERRLOG_PERSIST_PATH); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 251 | errorPath /= std::to_string(entryId); |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 252 | fs::remove(errorPath); |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 253 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 254 | auto removeId = [](std::list<uint32_t>& ids, uint32_t id) { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 255 | auto it = std::find(ids.begin(), ids.end(), id); |
| 256 | if (it != ids.end()) |
| 257 | { |
| 258 | ids.erase(it); |
| 259 | } |
| 260 | }; |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 261 | if (entryFound->second->severity() >= Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 262 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 263 | removeId(infoErrors, entryId); |
| 264 | } |
| 265 | else |
| 266 | { |
| 267 | removeId(realErrors, entryId); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 268 | } |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 269 | entries.erase(entryFound); |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 270 | } |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 271 | else |
| 272 | { |
| 273 | logging::log<level::ERR>("Invalid entry ID to delete", |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 274 | logging::entry("ID=%d", entryId)); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 275 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 276 | } |
| 277 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 278 | void Manager::restore() |
| 279 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 280 | auto sanity = [](const auto& id, const auto& restoredId) { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 281 | return id == restoredId; |
| 282 | }; |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 283 | std::vector<uint32_t> errorIds; |
| 284 | |
| 285 | fs::path dir(ERRLOG_PERSIST_PATH); |
| 286 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 287 | { |
| 288 | return; |
| 289 | } |
| 290 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 291 | for (auto& file : fs::directory_iterator(dir)) |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 292 | { |
| 293 | auto id = file.path().filename().c_str(); |
| 294 | auto idNum = std::stol(id); |
| 295 | auto e = std::make_unique<Entry>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 296 | busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this); |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 297 | if (deserialize(file.path(), *e)) |
| 298 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 299 | // validate the restored error entry id |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 300 | if (sanity(static_cast<uint32_t>(idNum), e->id())) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 301 | { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 302 | e->emit_object_added(); |
| 303 | if (e->severity() >= Entry::sevLowerLimit) |
| 304 | { |
| 305 | infoErrors.push_back(idNum); |
| 306 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 307 | else |
| 308 | { |
| 309 | realErrors.push_back(idNum); |
| 310 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 311 | |
| 312 | entries.insert(std::make_pair(idNum, std::move(e))); |
| 313 | errorIds.push_back(idNum); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 314 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 315 | else |
| 316 | { |
| 317 | logging::log<logging::level::ERR>( |
| 318 | "Failed in sanity check while restoring error entry. " |
| 319 | "Ignoring error entry", |
| 320 | logging::entry("ID_NUM=%d", idNum), |
| 321 | logging::entry("ENTRY_ID=%d", e->id())); |
| 322 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 326 | if (!errorIds.empty()) |
| 327 | { |
| 328 | entryId = *(std::max_element(errorIds.begin(), errorIds.end())); |
| 329 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 330 | } |
| 331 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 332 | void Manager::journalSync() |
| 333 | { |
| 334 | bool syncRequested = false; |
| 335 | auto fd = -1; |
| 336 | auto rc = -1; |
| 337 | auto wd = -1; |
| 338 | auto bus = sdbusplus::bus::new_default(); |
| 339 | |
| 340 | auto start = |
| 341 | duration_cast<microseconds>(steady_clock::now().time_since_epoch()) |
| 342 | .count(); |
| 343 | |
| 344 | constexpr auto maxRetry = 2; |
| 345 | for (int i = 0; i < maxRetry; i++) |
| 346 | { |
| 347 | // Read timestamp from synced file |
| 348 | constexpr auto syncedPath = "/run/systemd/journal/synced"; |
| 349 | std::ifstream syncedFile(syncedPath); |
| 350 | if (syncedFile.fail()) |
| 351 | { |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 352 | // If the synced file doesn't exist, a sync request will create it. |
| 353 | if (errno != ENOENT) |
| 354 | { |
| 355 | log<level::ERR>("Failed to open journal synced file", |
| 356 | entry("FILENAME=%s", syncedPath), |
| 357 | entry("ERRNO=%d", errno)); |
| 358 | return; |
| 359 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 360 | } |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 361 | else |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 362 | { |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 363 | // Only read the synced file if it exists. |
| 364 | // See if a sync happened by now |
| 365 | std::string timestampStr; |
| 366 | std::getline(syncedFile, timestampStr); |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 367 | auto timestamp = std::stoll(timestampStr); |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 368 | if (timestamp >= start) |
| 369 | { |
| 370 | return; |
| 371 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | // Let's ask for a sync, but only once |
| 375 | if (!syncRequested) |
| 376 | { |
| 377 | syncRequested = true; |
| 378 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 379 | constexpr auto JOURNAL_UNIT = "systemd-journald.service"; |
| 380 | auto signal = SIGRTMIN + 1; |
| 381 | |
| 382 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 383 | SYSTEMD_INTERFACE, "KillUnit"); |
| 384 | method.append(JOURNAL_UNIT, "main", signal); |
| 385 | bus.call(method); |
| 386 | if (method.is_method_error()) |
| 387 | { |
| 388 | log<level::ERR>("Failed to kill journal service"); |
| 389 | return; |
| 390 | } |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | // Let's install the inotify watch, if we didn't do that yet. This watch |
| 395 | // monitors the syncedFile for when journald updates it with a newer |
| 396 | // timestamp. This means the journal has been flushed. |
| 397 | if (fd < 0) |
| 398 | { |
| 399 | fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); |
| 400 | if (fd < 0) |
| 401 | { |
| 402 | log<level::ERR>("Failed to create inotify watch", |
| 403 | entry("ERRNO=%d", errno)); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal"; |
| 408 | wd = inotify_add_watch(fd, JOURNAL_RUN_PATH, |
| 409 | IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR); |
| 410 | if (wd < 0) |
| 411 | { |
| 412 | log<level::ERR>("Failed to watch journal directory", |
| 413 | entry("PATH=%s", JOURNAL_RUN_PATH), |
| 414 | entry("ERRNO=%d", errno)); |
| 415 | close(fd); |
| 416 | return; |
| 417 | } |
| 418 | continue; |
| 419 | } |
| 420 | |
| 421 | // Let's wait until inotify reports an event |
| 422 | struct pollfd fds = { |
| 423 | .fd = fd, |
| 424 | .events = POLLIN, |
| 425 | }; |
| 426 | constexpr auto pollTimeout = 5; // 5 seconds |
| 427 | rc = poll(&fds, 1, pollTimeout * 1000); |
| 428 | if (rc < 0) |
| 429 | { |
| 430 | log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno), |
| 431 | entry("ERR=%s", strerror(-rc))); |
| 432 | inotify_rm_watch(fd, wd); |
| 433 | close(fd); |
| 434 | return; |
| 435 | } |
| 436 | else if (rc == 0) |
| 437 | { |
| 438 | log<level::INFO>("Poll timeout, no new journal synced data", |
| 439 | entry("TIMEOUT=%d", pollTimeout)); |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | // Read from the specified file descriptor until there is no new data, |
| 444 | // throwing away everything read since the timestamp will be read at the |
| 445 | // beginning of the loop. |
| 446 | constexpr auto maxBytes = 64; |
| 447 | uint8_t buffer[maxBytes]; |
| 448 | while (read(fd, buffer, maxBytes) > 0) |
| 449 | ; |
| 450 | } |
| 451 | |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 452 | if (fd != -1) |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 453 | { |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 454 | if (wd != -1) |
| 455 | { |
| 456 | inotify_rm_watch(fd, wd); |
| 457 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 458 | close(fd); |
| 459 | } |
| 460 | |
| 461 | return; |
| 462 | } |
| 463 | |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 464 | std::string Manager::readFWVersion() |
| 465 | { |
| 466 | std::string version; |
| 467 | std::ifstream versionFile{BMC_VERSION_FILE}; |
| 468 | std::string line; |
| 469 | static constexpr auto VERSION_ID = "VERSION_ID="; |
| 470 | |
| 471 | while (std::getline(versionFile, line)) |
| 472 | { |
| 473 | if (line.find(VERSION_ID) != std::string::npos) |
| 474 | { |
| 475 | auto pos = line.find_first_of('"') + 1; |
| 476 | version = line.substr(pos, line.find_last_of('"') - pos); |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (version.empty()) |
| 482 | { |
| 483 | log<level::ERR>("Unable to read BMC firmware version"); |
| 484 | } |
| 485 | |
| 486 | return version; |
| 487 | } |
| 488 | |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 489 | } // namespace internal |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 490 | } // namespace logging |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 491 | } // namespace phosphor |