blob: 4cdbc4e23955a359683ac081963a95a953f76243 [file] [log] [blame]
Patrick Venturef18bf832018-10-26 18:14:00 -07001#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 Spinler99c2b402019-05-23 14:29:16 -05008#include "extensions.hpp"
Matt Spinlerf61f2922020-06-23 11:32:49 -05009#include "util.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -070010
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050011#include <systemd/sd-bus.h>
Adriana Kobylakd311bc82016-10-16 09:54:40 -050012#include <systemd/sd-journal.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050013#include <unistd.h>
Patrick Venturef18bf832018-10-26 18:14:00 -070014
Patrick Williams2544b412022-10-04 08:41:06 -050015#include <phosphor-logging/lg2.hpp>
16#include <sdbusplus/vtable.hpp>
17#include <xyz/openbmc_project/State/Host/server.hpp>
18
Matt Spinler99c2b402019-05-23 14:29:16 -050019#include <cassert>
Patrick Venturef18bf832018-10-26 18:14:00 -070020#include <chrono>
21#include <cstdio>
Patrick Venture30047bf2018-11-01 18:52:15 -070022#include <cstring>
Patrick Venturef18bf832018-10-26 18:14:00 -070023#include <fstream>
Patrick Venture30047bf2018-11-01 18:52:15 -070024#include <functional>
Patrick Venturef18bf832018-10-26 18:14:00 -070025#include <future>
26#include <iostream>
Patrick Venture30047bf2018-11-01 18:52:15 -070027#include <map>
Patrick Venturef18bf832018-10-26 18:14:00 -070028#include <set>
29#include <string>
Patrick Williamsb01a5b42021-08-28 15:11:45 -050030#include <string_view>
Patrick Venturef18bf832018-10-26 18:14:00 -070031#include <vector>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060032
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050033using namespace std::chrono;
Patrick Williams5f285c52021-07-27 21:25:38 -050034extern const std::map<
35 phosphor::logging::metadata::Metadata,
36 std::function<phosphor::logging::metadata::associations::Type>>
Patrick Venturef18bf832018-10-26 18:14:00 -070037 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050038
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060039namespace phosphor
40{
41namespace logging
42{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050043namespace internal
44{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050045inline auto getLevel(const std::string& errMsg)
46{
47 auto reqLevel = Entry::Level::Error; // Default to Error
48
49 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050050 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050051 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050052 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050053 }
54
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050055 return reqLevel;
56}
57
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050058int Manager::getRealErrSize()
59{
60 return realErrors.size();
61}
62
63int Manager::getInfoErrSize()
64{
65 return infoErrors.size();
66}
67
Lei YUb50c7052021-01-21 16:02:26 +080068uint32_t Manager::commit(uint64_t transactionId, std::string errMsg)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050069{
70 auto level = getLevel(errMsg);
71 _commit(transactionId, std::move(errMsg), level);
Lei YUb50c7052021-01-21 16:02:26 +080072 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050073}
74
Lei YUb50c7052021-01-21 16:02:26 +080075uint32_t Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
76 uint32_t errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050077{
78 _commit(transactionId, std::move(errMsg),
79 static_cast<Entry::Level>(errLvl));
Lei YUb50c7052021-01-21 16:02:26 +080080 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050081}
82
Patrick Williamsa5171972021-04-16 20:10:01 -050083void Manager::_commit(uint64_t transactionId [[maybe_unused]],
84 std::string&& errMsg, Entry::Level errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050085{
Patrick Williamsa5171972021-04-16 20:10:01 -050086 std::vector<std::string> additionalData{};
87
88 // When running as a test-case, the system may have a LOT of journal
89 // data and we may not have permissions to do some of the journal sync
90 // operations. Just skip over them.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070091 if (!IS_UNIT_TEST)
Adriana Kobylakd311bc82016-10-16 09:54:40 -050092 {
Patrick Williamsb01a5b42021-08-28 15:11:45 -050093 static constexpr auto transactionIdVar =
94 std::string_view{"TRANSACTION_ID"};
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070095 // Length of 'TRANSACTION_ID' string.
Patrick Williamsb01a5b42021-08-28 15:11:45 -050096 static constexpr auto transactionIdVarSize = transactionIdVar.size();
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070097 // Length of 'TRANSACTION_ID=' string.
Patrick Williamsb01a5b42021-08-28 15:11:45 -050098 static constexpr auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylakd311bc82016-10-16 09:54:40 -050099
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700100 // Flush all the pending log messages into the journal
Matt Spinler271d1432023-01-18 13:58:05 -0600101 util::journalSync();
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600102
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700103 sd_journal* j = nullptr;
104 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600105 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600106 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500107 lg2::error("Failed to open journal: {ERROR}", "ERROR",
108 strerror(-rc));
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700109 return;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600110 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600111
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700112 std::string transactionIdStr = std::to_string(transactionId);
113 std::set<std::string> metalist;
114 auto metamap = g_errMetaMap.find(errMsg);
115 if (metamap != g_errMetaMap.end())
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600116 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700117 metalist.insert(metamap->second.begin(), metamap->second.end());
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600118 }
119
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700120 // Add _PID field information in AdditionalData.
121 metalist.insert("_PID");
122
123 // Read the journal from the end to get the most recent entry first.
124 // The result from the sd_journal_get_data() is of the form
125 // VARIABLE=value.
126 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600127 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700128 const char* data = nullptr;
129 size_t length = 0;
130
131 // Look for the transaction id metadata variable
Patrick Williamsb01a5b42021-08-28 15:11:45 -0500132 rc = sd_journal_get_data(j, transactionIdVar.data(),
133 (const void**)&data, &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600134 if (rc < 0)
135 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700136 // This journal entry does not have the TRANSACTION_ID
137 // metadata variable.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600138 continue;
139 }
140
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700141 // journald does not guarantee that sd_journal_get_data() returns
142 // NULL terminated strings, so need to specify the size to use to
143 // compare, use the returned length instead of anything that relies
144 // on NULL terminators like strlen(). The data variable is in the
145 // form of 'TRANSACTION_ID=1234'. Remove the TRANSACTION_ID
146 // characters plus the (=) sign to do the comparison. 'data +
147 // transactionIdVarOffset' will be in the form of '1234'. 'length -
148 // transactionIdVarOffset' will be the length of '1234'.
149 if ((length <= (transactionIdVarOffset)) ||
150 (transactionIdStr.compare(
151 0, transactionIdStr.size(), data + transactionIdVarOffset,
152 length - transactionIdVarOffset) != 0))
153 {
154 // The value of the TRANSACTION_ID metadata is not the requested
155 // transaction id number.
156 continue;
157 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600158
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700159 // Search for all metadata variables in the current journal entry.
160 for (auto i = metalist.cbegin(); i != metalist.cend();)
161 {
162 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
163 &length);
164 if (rc < 0)
165 {
166 // Metadata variable not found, check next metadata
167 // variable.
168 i++;
169 continue;
170 }
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500171
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700172 // Metadata variable found, save it and remove it from the set.
173 additionalData.emplace_back(data, length);
174 i = metalist.erase(i);
175 }
176 if (metalist.empty())
177 {
178 // All metadata variables found, break out of journal loop.
179 break;
180 }
181 }
182 if (!metalist.empty())
183 {
184 // Not all the metadata variables were found in the journal.
185 for (auto& metaVarStr : metalist)
186 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500187 lg2::info("Failed to find metadata: {META_FIELD}", "META_FIELD",
188 metaVarStr);
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700189 }
190 }
191
192 sd_journal_close(j);
193 }
Matt Spinlerb60e7552019-07-24 15:28:08 -0500194 createEntry(errMsg, errLvl, additionalData);
195}
196
197void Manager::createEntry(std::string errMsg, Entry::Level errLvl,
Matt Spinlerc64b7122020-03-26 10:55:01 -0500198 std::vector<std::string> additionalData,
199 const FFDCEntries& ffdc)
Matt Spinlerb60e7552019-07-24 15:28:08 -0500200{
201 if (!Extensions::disableDefaultLogCaps())
202 {
203 if (errLvl < Entry::sevLowerLimit)
204 {
205 if (realErrors.size() >= ERROR_CAP)
206 {
207 erase(realErrors.front());
208 }
209 }
210 else
211 {
212 if (infoErrors.size() >= ERROR_INFO_CAP)
213 {
214 erase(infoErrors.front());
215 }
216 }
217 }
218
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600219 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500220 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500221 {
222 infoErrors.push_back(entryId);
223 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600224 else
225 {
226 realErrors.push_back(entryId);
227 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600228 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700229 std::chrono::system_clock::now().time_since_epoch())
230 .count();
231 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600232
Patrick Venturef18bf832018-10-26 18:14:00 -0700233 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600234 processMetadata(errMsg, additionalData, objects);
235
Matt Spinlerfb978da2022-01-21 08:42:24 -0600236 auto e = std::make_unique<Entry>(
237 busLog, objPath, entryId,
238 ms, // Milliseconds since 1970
239 errLvl, std::move(errMsg), std::move(additionalData),
240 std::move(objects), fwVersion, getEntrySerializePath(entryId), *this);
241
242 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500243
Matt Spinler601b8112022-01-26 13:18:04 -0600244 if (isQuiesceOnErrorEnabled() && (errLvl < Entry::sevLowerLimit) &&
245 isCalloutPresent(*e))
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500246 {
Andrew Geissler32874542020-07-09 09:17:03 -0500247 quiesceOnError(entryId);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500248 }
249
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600250 // Add entry before calling the extensions so that they have access to it
251 entries.insert(std::make_pair(entryId, std::move(e)));
252
253 doExtensionLogCreate(*entries.find(entryId)->second, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500254
255 // Note: No need to close the file descriptors in the FFDC.
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500256}
257
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500258bool Manager::isQuiesceOnErrorEnabled()
259{
Patrick Williamsa5171972021-04-16 20:10:01 -0500260 // When running under tests, the Logging.Settings service will not be
261 // present. Assume false.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700262 if (IS_UNIT_TEST)
263 {
264 return false;
265 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500266
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500267 std::variant<bool> property;
268
269 auto method = this->busLog.new_method_call(
270 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
271 "org.freedesktop.DBus.Properties", "Get");
272
273 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
274
275 try
276 {
277 auto reply = this->busLog.call(method);
278 reply.read(property);
279 }
Patrick Williams45e83522022-07-22 19:26:52 -0500280 catch (const sdbusplus::exception_t& e)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500281 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500282 lg2::error("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
283 e);
Matt Spinler7ba17c32023-06-12 09:20:15 -0500284 return false;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500285 }
286
287 return std::get<bool>(property);
288}
289
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500290bool Manager::isCalloutPresent(const Entry& entry)
291{
292 for (const auto& c : entry.additionalData())
293 {
294 if (c.find("CALLOUT_") != std::string::npos)
295 {
296 return true;
297 }
298 }
299
300 return false;
301}
302
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500303void Manager::findAndRemoveResolvedBlocks()
304{
305 for (auto& entry : entries)
306 {
307 if (entry.second->resolved())
308 {
309 checkAndRemoveBlockingError(entry.first);
310 }
311 }
312}
313
Patrick Williams45e83522022-07-22 19:26:52 -0500314void Manager::onEntryResolve(sdbusplus::message_t& msg)
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500315{
316 using Interface = std::string;
317 using Property = std::string;
318 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500319 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500320
321 Interface interface;
322 Properties properties;
323
324 msg.read(interface, properties);
325
326 for (const auto& p : properties)
327 {
328 if (p.first == "Resolved")
329 {
330 findAndRemoveResolvedBlocks();
331 return;
332 }
333 }
334}
335
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500336void Manager::checkAndQuiesceHost()
337{
Willy Tu6ddbf692023-09-05 10:54:16 -0700338 using Host = sdbusplus::server::xyz::openbmc_project::state::Host;
Patrick Williamsa144a272021-07-16 17:03:55 -0500339
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500340 // First check host state
Patrick Williamsa144a272021-07-16 17:03:55 -0500341 std::variant<Host::HostState> property;
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500342
343 auto method = this->busLog.new_method_call(
344 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
345 "org.freedesktop.DBus.Properties", "Get");
346
347 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
348
349 try
350 {
351 auto reply = this->busLog.call(method);
352 reply.read(property);
353 }
Patrick Williams45e83522022-07-22 19:26:52 -0500354 catch (const sdbusplus::exception_t& e)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500355 {
356 // Quiescing the host is a "best effort" type function. If unable to
357 // read the host state or it comes back empty, just return.
358 // The boot block object will still be created and the associations to
359 // find the log will be present. Don't want a dependency with
360 // phosphor-state-manager service
Patrick Williams5f285c52021-07-27 21:25:38 -0500361 lg2::info("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
362 e);
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500363 return;
364 }
365
Patrick Williamsa144a272021-07-16 17:03:55 -0500366 auto hostState = std::get<Host::HostState>(property);
367 if (hostState != Host::HostState::Running)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500368 {
369 return;
370 }
371
372 auto quiesce = this->busLog.new_method_call(
373 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
374 "org.freedesktop.systemd1.Manager", "StartUnit");
375
Andrew Geissler86a60e92022-04-15 14:36:02 -0500376 quiesce.append("obmc-host-graceful-quiesce@0.target");
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500377 quiesce.append("replace");
378
379 this->busLog.call_noreply(quiesce);
380}
381
Andrew Geissler32874542020-07-09 09:17:03 -0500382void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500383{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500384 // Verify we don't already have this entry blocking
Matt Spinler4a375952022-07-01 11:15:33 -0500385 auto it = find_if(this->blockingErrors.begin(), this->blockingErrors.end(),
386 [&](const std::unique_ptr<Block>& obj) {
Patrick Williamsac1ba3f2023-05-10 07:50:16 -0500387 return obj->entryId == entryId;
388 });
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500389 if (it != this->blockingErrors.end())
390 {
391 // Already recorded so just return
Patrick Williams5f285c52021-07-27 21:25:38 -0500392 lg2::debug(
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500393 "QuiesceOnError set and callout present but entry already logged");
394 return;
395 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500396
Patrick Williams5f285c52021-07-27 21:25:38 -0500397 lg2::info("QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500398
Patrick Williams2544b412022-10-04 08:41:06 -0500399 auto blockPath = std::string(OBJ_LOGGING) + "/block" +
400 std::to_string(entryId);
Andrew Geissler32874542020-07-09 09:17:03 -0500401 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500402 this->blockingErrors.push_back(std::move(blockObj));
403
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500404 // Register call back if log is resolved
405 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500406 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Patrick Williams45e83522022-07-22 19:26:52 -0500407 auto callback = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500408 this->busLog,
409 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
410 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
411 std::placeholders::_1));
412
413 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500414 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500415
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500416 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500417}
418
Matt Spinlerc64b7122020-03-26 10:55:01 -0500419void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500420{
421 // Make the association <endpointpath>/<endpointtype> paths
422 std::vector<std::string> assocs;
423 for (const auto& [forwardType, reverseType, endpoint] :
424 entry.associations())
425 {
426 std::string e{endpoint};
427 e += '/' + reverseType;
428 assocs.push_back(e);
429 }
430
431 for (auto& create : Extensions::getCreateFunctions())
432 {
433 try
434 {
435 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500436 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500437 }
Patrick Williams66491c62021-10-06 12:23:37 -0500438 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500439 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500440 lg2::error(
441 "An extension's create function threw an exception: {ERROR}",
442 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500443 }
444 }
445}
446
Patrick Williamsf40323d2021-04-16 15:35:17 -0500447void Manager::processMetadata(const std::string& /*errorName*/,
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600448 const std::vector<std::string>& additionalData,
449 AssociationList& objects) const
450{
451 // additionalData is a list of "metadata=value"
452 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700453 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600454 {
Patrick Venture34438962018-10-30 13:17:37 -0700455 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700456 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600457 {
Patrick Venture34438962018-10-30 13:17:37 -0700458 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600459 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700460 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600461 {
462 (iter->second)(metadata, additionalData, objects);
463 }
464 }
465 }
466}
467
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500468void Manager::checkAndRemoveBlockingError(uint32_t entryId)
469{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500470 // First look for blocking object and remove
Matt Spinler4a375952022-07-01 11:15:33 -0500471 auto it = find_if(blockingErrors.begin(), blockingErrors.end(),
472 [&](const std::unique_ptr<Block>& obj) {
Patrick Williamsac1ba3f2023-05-10 07:50:16 -0500473 return obj->entryId == entryId;
474 });
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500475 if (it != blockingErrors.end())
476 {
477 blockingErrors.erase(it);
478 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500479
480 // Now remove the callback looking for the error to be resolved
481 auto resolveFind = propChangedEntryCallback.find(entryId);
482 if (resolveFind != propChangedEntryCallback.end())
483 {
484 propChangedEntryCallback.erase(resolveFind);
485 }
486
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500487 return;
488}
489
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500490void Manager::erase(uint32_t entryId)
491{
Patrick Venture34438962018-10-30 13:17:37 -0700492 auto entryFound = entries.find(entryId);
493 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500494 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500495 for (auto& func : Extensions::getDeleteProhibitedFunctions())
496 {
497 try
498 {
499 bool prohibited = false;
500 func(entryId, prohibited);
501 if (prohibited)
502 {
503 // Future work remains to throw an error here.
504 return;
505 }
506 }
Patrick Williams66491c62021-10-06 12:23:37 -0500507 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500508 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500509 lg2::error("An extension's deleteProhibited function threw an "
510 "exception: {ERROR}",
511 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500512 }
513 }
514
Deepak Kodihalli33887992017-06-13 07:06:49 -0500515 // Delete the persistent representation of this error.
516 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600517 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500518 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600519
Patrick Venturef18bf832018-10-26 18:14:00 -0700520 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600521 auto it = std::find(ids.begin(), ids.end(), id);
522 if (it != ids.end())
523 {
524 ids.erase(it);
525 }
526 };
Patrick Venture34438962018-10-30 13:17:37 -0700527 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500528 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600529 removeId(infoErrors, entryId);
530 }
531 else
532 {
533 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500534 }
Patrick Venture34438962018-10-30 13:17:37 -0700535 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500536
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500537 checkAndRemoveBlockingError(entryId);
538
Matt Spinler99c2b402019-05-23 14:29:16 -0500539 for (auto& remove : Extensions::getDeleteFunctions())
540 {
541 try
542 {
543 remove(entryId);
544 }
Patrick Williams66491c62021-10-06 12:23:37 -0500545 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500546 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500547 lg2::error("An extension's delete function threw an exception: "
548 "{ERROR}",
549 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500550 }
551 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500552 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600553 else
554 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500555 lg2::error("Invalid entry ID ({ID}) to delete", "ID", entryId);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600556 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500557}
558
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500559void Manager::restore()
560{
Patrick Venturef18bf832018-10-26 18:14:00 -0700561 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600562 return id == restoredId;
563 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500564
565 fs::path dir(ERRLOG_PERSIST_PATH);
566 if (!fs::exists(dir) || fs::is_empty(dir))
567 {
568 return;
569 }
570
Patrick Venturef18bf832018-10-26 18:14:00 -0700571 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500572 {
573 auto id = file.path().filename().c_str();
574 auto idNum = std::stol(id);
575 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700576 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500577 if (deserialize(file.path(), *e))
578 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700579 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600580 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500581 {
Matt Spinleref952af2021-08-19 10:23:05 -0500582 e->path(file.path(), true);
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600583 if (e->severity() >= Entry::sevLowerLimit)
584 {
585 infoErrors.push_back(idNum);
586 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600587 else
588 {
589 realErrors.push_back(idNum);
590 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600591
592 entries.insert(std::make_pair(idNum, std::move(e)));
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500593 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600594 else
595 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500596 lg2::error(
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600597 "Failed in sanity check while restoring error entry. "
Patrick Williams5f285c52021-07-27 21:25:38 -0500598 "Ignoring error entry {ID_NUM}/{ENTRY_ID}.",
599 "ID_NUM", idNum, "ENTRY_ID", e->id());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600600 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500601 }
602 }
603
Lei YU01bf5c42022-05-23 15:33:23 +0800604 if (!entries.empty())
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530605 {
Lei YU01bf5c42022-05-23 15:33:23 +0800606 entryId = entries.rbegin()->first;
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530607 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500608}
609
Matt Spinler1275bd12018-05-01 15:13:53 -0500610std::string Manager::readFWVersion()
611{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500612 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500613
Matt Spinlerf61f2922020-06-23 11:32:49 -0500614 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500615 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500616 lg2::error("Unable to read BMC firmware version");
Matt Spinler1275bd12018-05-01 15:13:53 -0500617 }
618
Matt Spinlerf61f2922020-06-23 11:32:49 -0500619 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500620}
621
Matt Spinler3fb83b32019-07-26 11:22:44 -0500622void Manager::create(const std::string& message, Entry::Level severity,
Paul Fertser221b79b2024-03-04 15:40:23 +0000623 const std::map<std::string, std::string>& additionalData,
624 const FFDCEntries& ffdc)
Matt Spinlerc64b7122020-03-26 10:55:01 -0500625{
626 // Convert the map into a vector of "key=value" strings
627 std::vector<std::string> ad;
628 metadata::associations::combine(additionalData, ad);
629
630 createEntry(message, severity, ad, ffdc);
631}
632
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500633} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600634} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700635} // namespace phosphor