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