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, |
| 188 | std::vector<std::string> additionalData) |
| 189 | { |
| 190 | if (!Extensions::disableDefaultLogCaps()) |
| 191 | { |
| 192 | if (errLvl < Entry::sevLowerLimit) |
| 193 | { |
| 194 | if (realErrors.size() >= ERROR_CAP) |
| 195 | { |
| 196 | erase(realErrors.front()); |
| 197 | } |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | if (infoErrors.size() >= ERROR_INFO_CAP) |
| 202 | { |
| 203 | erase(infoErrors.front()); |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 208 | entryId++; |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 209 | if (errLvl >= Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 210 | { |
| 211 | infoErrors.push_back(entryId); |
| 212 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 213 | else |
| 214 | { |
| 215 | realErrors.push_back(entryId); |
| 216 | } |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 217 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 218 | std::chrono::system_clock::now().time_since_epoch()) |
| 219 | .count(); |
| 220 | auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 221 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 222 | AssociationList objects{}; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 223 | processMetadata(errMsg, additionalData, objects); |
| 224 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 225 | auto e = std::make_unique<Entry>(busLog, objPath, entryId, |
| 226 | ms, // Milliseconds since 1970 |
| 227 | errLvl, std::move(errMsg), |
| 228 | std::move(additionalData), |
| 229 | std::move(objects), fwVersion, *this); |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 230 | serialize(*e); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 231 | |
| 232 | doExtensionLogCreate(*e); |
| 233 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 234 | entries.insert(std::make_pair(entryId, std::move(e))); |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 235 | } |
| 236 | |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 237 | void Manager::doExtensionLogCreate(const Entry& entry) |
| 238 | { |
| 239 | // Make the association <endpointpath>/<endpointtype> paths |
| 240 | std::vector<std::string> assocs; |
| 241 | for (const auto& [forwardType, reverseType, endpoint] : |
| 242 | entry.associations()) |
| 243 | { |
| 244 | std::string e{endpoint}; |
| 245 | e += '/' + reverseType; |
| 246 | assocs.push_back(e); |
| 247 | } |
| 248 | |
| 249 | for (auto& create : Extensions::getCreateFunctions()) |
| 250 | { |
| 251 | try |
| 252 | { |
| 253 | create(entry.message(), entry.id(), entry.timestamp(), |
| 254 | entry.severity(), entry.additionalData(), assocs); |
| 255 | } |
| 256 | catch (std::exception& e) |
| 257 | { |
| 258 | log<level::ERR>("An extension's create function threw an exception", |
| 259 | phosphor::logging::entry("ERROR=%s", e.what())); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 264 | void Manager::processMetadata(const std::string& errorName, |
| 265 | const std::vector<std::string>& additionalData, |
| 266 | AssociationList& objects) const |
| 267 | { |
| 268 | // additionalData is a list of "metadata=value" |
| 269 | constexpr auto separator = '='; |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 270 | for (const auto& entryItem : additionalData) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 271 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 272 | auto found = entryItem.find(separator); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 273 | if (std::string::npos != found) |
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 metadata = entryItem.substr(0, found); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 276 | auto iter = meta.find(metadata); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 277 | if (meta.end() != iter) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 278 | { |
| 279 | (iter->second)(metadata, additionalData, objects); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 285 | void Manager::erase(uint32_t entryId) |
| 286 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 287 | auto entryFound = entries.find(entryId); |
| 288 | if (entries.end() != entryFound) |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 289 | { |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 290 | for (auto& func : Extensions::getDeleteProhibitedFunctions()) |
| 291 | { |
| 292 | try |
| 293 | { |
| 294 | bool prohibited = false; |
| 295 | func(entryId, prohibited); |
| 296 | if (prohibited) |
| 297 | { |
| 298 | // Future work remains to throw an error here. |
| 299 | return; |
| 300 | } |
| 301 | } |
| 302 | catch (std::exception& e) |
| 303 | { |
| 304 | log<level::ERR>( |
| 305 | "An extension's deleteProhibited function threw " |
| 306 | "an exception", |
| 307 | entry("ERROR=%s", e.what())); |
| 308 | } |
| 309 | } |
| 310 | |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 311 | // Delete the persistent representation of this error. |
| 312 | fs::path errorPath(ERRLOG_PERSIST_PATH); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 313 | errorPath /= std::to_string(entryId); |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 314 | fs::remove(errorPath); |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 315 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 316 | auto removeId = [](std::list<uint32_t>& ids, uint32_t id) { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 317 | auto it = std::find(ids.begin(), ids.end(), id); |
| 318 | if (it != ids.end()) |
| 319 | { |
| 320 | ids.erase(it); |
| 321 | } |
| 322 | }; |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 323 | if (entryFound->second->severity() >= Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 324 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 325 | removeId(infoErrors, entryId); |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | removeId(realErrors, entryId); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 330 | } |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 331 | entries.erase(entryFound); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 332 | |
| 333 | for (auto& remove : Extensions::getDeleteFunctions()) |
| 334 | { |
| 335 | try |
| 336 | { |
| 337 | remove(entryId); |
| 338 | } |
| 339 | catch (std::exception& e) |
| 340 | { |
| 341 | log<level::ERR>("An extension's delete function threw an " |
| 342 | "exception", |
| 343 | entry("ERROR=%s", e.what())); |
| 344 | } |
| 345 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 346 | } |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 347 | else |
| 348 | { |
| 349 | logging::log<level::ERR>("Invalid entry ID to delete", |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 350 | logging::entry("ID=%d", entryId)); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 351 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 352 | } |
| 353 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 354 | void Manager::restore() |
| 355 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 356 | auto sanity = [](const auto& id, const auto& restoredId) { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 357 | return id == restoredId; |
| 358 | }; |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 359 | std::vector<uint32_t> errorIds; |
| 360 | |
| 361 | fs::path dir(ERRLOG_PERSIST_PATH); |
| 362 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 363 | { |
| 364 | return; |
| 365 | } |
| 366 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 367 | for (auto& file : fs::directory_iterator(dir)) |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 368 | { |
| 369 | auto id = file.path().filename().c_str(); |
| 370 | auto idNum = std::stol(id); |
| 371 | auto e = std::make_unique<Entry>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 372 | busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this); |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 373 | if (deserialize(file.path(), *e)) |
| 374 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 375 | // validate the restored error entry id |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 376 | if (sanity(static_cast<uint32_t>(idNum), e->id())) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 377 | { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 378 | e->emit_object_added(); |
| 379 | if (e->severity() >= Entry::sevLowerLimit) |
| 380 | { |
| 381 | infoErrors.push_back(idNum); |
| 382 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 383 | else |
| 384 | { |
| 385 | realErrors.push_back(idNum); |
| 386 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 387 | |
| 388 | entries.insert(std::make_pair(idNum, std::move(e))); |
| 389 | errorIds.push_back(idNum); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 390 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 391 | else |
| 392 | { |
| 393 | logging::log<logging::level::ERR>( |
| 394 | "Failed in sanity check while restoring error entry. " |
| 395 | "Ignoring error entry", |
| 396 | logging::entry("ID_NUM=%d", idNum), |
| 397 | logging::entry("ENTRY_ID=%d", e->id())); |
| 398 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 402 | if (!errorIds.empty()) |
| 403 | { |
| 404 | entryId = *(std::max_element(errorIds.begin(), errorIds.end())); |
| 405 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 406 | } |
| 407 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 408 | void Manager::journalSync() |
| 409 | { |
| 410 | bool syncRequested = false; |
| 411 | auto fd = -1; |
| 412 | auto rc = -1; |
| 413 | auto wd = -1; |
| 414 | auto bus = sdbusplus::bus::new_default(); |
| 415 | |
| 416 | auto start = |
| 417 | duration_cast<microseconds>(steady_clock::now().time_since_epoch()) |
| 418 | .count(); |
| 419 | |
Matthew Barth | 5991389 | 2019-08-27 15:43:48 -0500 | [diff] [blame] | 420 | // Each time an error log is committed, a request to sync the journal |
| 421 | // must occur and block that error log commit until it completes. A 5sec |
| 422 | // block is done to allow sufficient time for the journal to be synced. |
| 423 | // |
| 424 | // Number of loop iterations = 3 for the following reasons: |
| 425 | // Iteration #1: Requests a journal sync by killing the journald service. |
| 426 | // Iteration #2: Setup an inotify watch to monitor the synced file that |
| 427 | // journald updates with the timestamp the last time the |
| 428 | // journal was flushed. |
| 429 | // Iteration #3: Poll to wait until inotify reports an event which blocks |
| 430 | // the error log from being commited until the sync completes. |
| 431 | constexpr auto maxRetry = 3; |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 432 | for (int i = 0; i < maxRetry; i++) |
| 433 | { |
| 434 | // Read timestamp from synced file |
| 435 | constexpr auto syncedPath = "/run/systemd/journal/synced"; |
| 436 | std::ifstream syncedFile(syncedPath); |
| 437 | if (syncedFile.fail()) |
| 438 | { |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 439 | // If the synced file doesn't exist, a sync request will create it. |
| 440 | if (errno != ENOENT) |
| 441 | { |
| 442 | log<level::ERR>("Failed to open journal synced file", |
| 443 | entry("FILENAME=%s", syncedPath), |
| 444 | entry("ERRNO=%d", errno)); |
| 445 | return; |
| 446 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 447 | } |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 448 | else |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 449 | { |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 450 | // Only read the synced file if it exists. |
| 451 | // See if a sync happened by now |
| 452 | std::string timestampStr; |
| 453 | std::getline(syncedFile, timestampStr); |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 454 | auto timestamp = std::stoll(timestampStr); |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 455 | if (timestamp >= start) |
| 456 | { |
| 457 | return; |
| 458 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Let's ask for a sync, but only once |
| 462 | if (!syncRequested) |
| 463 | { |
| 464 | syncRequested = true; |
| 465 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 466 | constexpr auto JOURNAL_UNIT = "systemd-journald.service"; |
| 467 | auto signal = SIGRTMIN + 1; |
| 468 | |
| 469 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 470 | SYSTEMD_INTERFACE, "KillUnit"); |
| 471 | method.append(JOURNAL_UNIT, "main", signal); |
| 472 | bus.call(method); |
| 473 | if (method.is_method_error()) |
| 474 | { |
| 475 | log<level::ERR>("Failed to kill journal service"); |
| 476 | return; |
| 477 | } |
| 478 | continue; |
| 479 | } |
| 480 | |
| 481 | // Let's install the inotify watch, if we didn't do that yet. This watch |
| 482 | // monitors the syncedFile for when journald updates it with a newer |
| 483 | // timestamp. This means the journal has been flushed. |
| 484 | if (fd < 0) |
| 485 | { |
| 486 | fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); |
| 487 | if (fd < 0) |
| 488 | { |
| 489 | log<level::ERR>("Failed to create inotify watch", |
| 490 | entry("ERRNO=%d", errno)); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal"; |
| 495 | wd = inotify_add_watch(fd, JOURNAL_RUN_PATH, |
| 496 | IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR); |
| 497 | if (wd < 0) |
| 498 | { |
| 499 | log<level::ERR>("Failed to watch journal directory", |
| 500 | entry("PATH=%s", JOURNAL_RUN_PATH), |
| 501 | entry("ERRNO=%d", errno)); |
| 502 | close(fd); |
| 503 | return; |
| 504 | } |
| 505 | continue; |
| 506 | } |
| 507 | |
| 508 | // Let's wait until inotify reports an event |
| 509 | struct pollfd fds = { |
| 510 | .fd = fd, |
| 511 | .events = POLLIN, |
| 512 | }; |
| 513 | constexpr auto pollTimeout = 5; // 5 seconds |
| 514 | rc = poll(&fds, 1, pollTimeout * 1000); |
| 515 | if (rc < 0) |
| 516 | { |
| 517 | log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno), |
| 518 | entry("ERR=%s", strerror(-rc))); |
| 519 | inotify_rm_watch(fd, wd); |
| 520 | close(fd); |
| 521 | return; |
| 522 | } |
| 523 | else if (rc == 0) |
| 524 | { |
| 525 | log<level::INFO>("Poll timeout, no new journal synced data", |
| 526 | entry("TIMEOUT=%d", pollTimeout)); |
| 527 | break; |
| 528 | } |
| 529 | |
| 530 | // Read from the specified file descriptor until there is no new data, |
| 531 | // throwing away everything read since the timestamp will be read at the |
| 532 | // beginning of the loop. |
| 533 | constexpr auto maxBytes = 64; |
| 534 | uint8_t buffer[maxBytes]; |
| 535 | while (read(fd, buffer, maxBytes) > 0) |
| 536 | ; |
| 537 | } |
| 538 | |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 539 | if (fd != -1) |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 540 | { |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 541 | if (wd != -1) |
| 542 | { |
| 543 | inotify_rm_watch(fd, wd); |
| 544 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 545 | close(fd); |
| 546 | } |
| 547 | |
| 548 | return; |
| 549 | } |
| 550 | |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 551 | std::string Manager::readFWVersion() |
| 552 | { |
| 553 | std::string version; |
| 554 | std::ifstream versionFile{BMC_VERSION_FILE}; |
| 555 | std::string line; |
| 556 | static constexpr auto VERSION_ID = "VERSION_ID="; |
| 557 | |
| 558 | while (std::getline(versionFile, line)) |
| 559 | { |
| 560 | if (line.find(VERSION_ID) != std::string::npos) |
| 561 | { |
| 562 | auto pos = line.find_first_of('"') + 1; |
| 563 | version = line.substr(pos, line.find_last_of('"') - pos); |
| 564 | break; |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | if (version.empty()) |
| 569 | { |
| 570 | log<level::ERR>("Unable to read BMC firmware version"); |
| 571 | } |
| 572 | |
| 573 | return version; |
| 574 | } |
| 575 | |
Matt Spinler | 3fb83b3 | 2019-07-26 11:22:44 -0500 | [diff] [blame] | 576 | void Manager::create(const std::string& message, Entry::Level severity, |
| 577 | const std::map<std::string, std::string>& additionalData) |
| 578 | { |
| 579 | // Convert the map into a vector of "key=value" strings |
| 580 | std::vector<std::string> ad; |
| 581 | metadata::associations::combine(additionalData, ad); |
| 582 | |
| 583 | createEntry(message, severity, ad); |
| 584 | } |
| 585 | |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 586 | } // namespace internal |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 587 | } // namespace logging |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 588 | } // namespace phosphor |