blob: 1b0cf3f89e611d2e7bbd95407acc6befcf59857d [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"
Patrick Williamsfa2d9622024-09-30 16:25:43 -04009#include "paths.hpp"
Matt Spinlerf61f2922020-06-23 11:32:49 -050010#include "util.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -070011
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050012#include <systemd/sd-bus.h>
Adriana Kobylakd311bc82016-10-16 09:54:40 -050013#include <systemd/sd-journal.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050014#include <unistd.h>
Patrick Venturef18bf832018-10-26 18:14:00 -070015
Patrick Williams2544b412022-10-04 08:41:06 -050016#include <phosphor-logging/lg2.hpp>
17#include <sdbusplus/vtable.hpp>
18#include <xyz/openbmc_project/State/Host/server.hpp>
19
Matt Spinler99c2b402019-05-23 14:29:16 -050020#include <cassert>
Patrick Venturef18bf832018-10-26 18:14:00 -070021#include <chrono>
22#include <cstdio>
Patrick Venture30047bf2018-11-01 18:52:15 -070023#include <cstring>
Patrick Venturef18bf832018-10-26 18:14:00 -070024#include <fstream>
Patrick Venture30047bf2018-11-01 18:52:15 -070025#include <functional>
Patrick Venturef18bf832018-10-26 18:14:00 -070026#include <future>
27#include <iostream>
Patrick Venture30047bf2018-11-01 18:52:15 -070028#include <map>
Patrick Venturef18bf832018-10-26 18:14:00 -070029#include <set>
30#include <string>
Patrick Williamsb01a5b42021-08-28 15:11:45 -050031#include <string_view>
Patrick Venturef18bf832018-10-26 18:14:00 -070032#include <vector>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060033
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050034using namespace std::chrono;
Patrick Williams5f285c52021-07-27 21:25:38 -050035extern const std::map<
36 phosphor::logging::metadata::Metadata,
37 std::function<phosphor::logging::metadata::associations::Type>>
Patrick Venturef18bf832018-10-26 18:14:00 -070038 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050039
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060040namespace phosphor
41{
42namespace logging
43{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050044namespace internal
45{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050046inline auto getLevel(const std::string& errMsg)
47{
48 auto reqLevel = Entry::Level::Error; // Default to Error
49
50 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050051 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050052 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050053 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050054 }
55
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050056 return reqLevel;
57}
58
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050059int Manager::getRealErrSize()
60{
61 return realErrors.size();
62}
63
64int Manager::getInfoErrSize()
65{
66 return infoErrors.size();
67}
68
Lei YUb50c7052021-01-21 16:02:26 +080069uint32_t Manager::commit(uint64_t transactionId, std::string errMsg)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050070{
71 auto level = getLevel(errMsg);
72 _commit(transactionId, std::move(errMsg), level);
Lei YUb50c7052021-01-21 16:02:26 +080073 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050074}
75
Lei YUb50c7052021-01-21 16:02:26 +080076uint32_t Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
77 uint32_t errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050078{
79 _commit(transactionId, std::move(errMsg),
80 static_cast<Entry::Level>(errLvl));
Lei YUb50c7052021-01-21 16:02:26 +080081 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050082}
83
Patrick Williamsa5171972021-04-16 20:10:01 -050084void Manager::_commit(uint64_t transactionId [[maybe_unused]],
85 std::string&& errMsg, Entry::Level errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050086{
Patrick Williamsa5171972021-04-16 20:10:01 -050087 std::vector<std::string> additionalData{};
88
89 // When running as a test-case, the system may have a LOT of journal
90 // data and we may not have permissions to do some of the journal sync
91 // operations. Just skip over them.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070092 if (!IS_UNIT_TEST)
Adriana Kobylakd311bc82016-10-16 09:54:40 -050093 {
Patrick Williamsb01a5b42021-08-28 15:11:45 -050094 static constexpr auto transactionIdVar =
95 std::string_view{"TRANSACTION_ID"};
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070096 // Length of 'TRANSACTION_ID' string.
Patrick Williamsb01a5b42021-08-28 15:11:45 -050097 static constexpr auto transactionIdVarSize = transactionIdVar.size();
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070098 // Length of 'TRANSACTION_ID=' string.
Patrick Williamsb01a5b42021-08-28 15:11:45 -050099 static constexpr auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500100
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700101 // Flush all the pending log messages into the journal
Matt Spinler271d1432023-01-18 13:58:05 -0600102 util::journalSync();
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600103
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700104 sd_journal* j = nullptr;
105 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600106 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600107 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500108 lg2::error("Failed to open journal: {ERROR}", "ERROR",
109 strerror(-rc));
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700110 return;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600111 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600112
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700113 std::string transactionIdStr = std::to_string(transactionId);
114 std::set<std::string> metalist;
115 auto metamap = g_errMetaMap.find(errMsg);
116 if (metamap != g_errMetaMap.end())
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600117 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700118 metalist.insert(metamap->second.begin(), metamap->second.end());
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600119 }
120
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700121 // Add _PID field information in AdditionalData.
122 metalist.insert("_PID");
123
124 // Read the journal from the end to get the most recent entry first.
125 // The result from the sd_journal_get_data() is of the form
126 // VARIABLE=value.
127 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600128 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700129 const char* data = nullptr;
130 size_t length = 0;
131
132 // Look for the transaction id metadata variable
Patrick Williamsb01a5b42021-08-28 15:11:45 -0500133 rc = sd_journal_get_data(j, transactionIdVar.data(),
134 (const void**)&data, &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600135 if (rc < 0)
136 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700137 // This journal entry does not have the TRANSACTION_ID
138 // metadata variable.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600139 continue;
140 }
141
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700142 // journald does not guarantee that sd_journal_get_data() returns
143 // NULL terminated strings, so need to specify the size to use to
144 // compare, use the returned length instead of anything that relies
145 // on NULL terminators like strlen(). The data variable is in the
146 // form of 'TRANSACTION_ID=1234'. Remove the TRANSACTION_ID
147 // characters plus the (=) sign to do the comparison. 'data +
148 // transactionIdVarOffset' will be in the form of '1234'. 'length -
149 // transactionIdVarOffset' will be the length of '1234'.
150 if ((length <= (transactionIdVarOffset)) ||
151 (transactionIdStr.compare(
152 0, transactionIdStr.size(), data + transactionIdVarOffset,
153 length - transactionIdVarOffset) != 0))
154 {
155 // The value of the TRANSACTION_ID metadata is not the requested
156 // transaction id number.
157 continue;
158 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600159
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700160 // Search for all metadata variables in the current journal entry.
161 for (auto i = metalist.cbegin(); i != metalist.cend();)
162 {
163 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
164 &length);
165 if (rc < 0)
166 {
167 // Metadata variable not found, check next metadata
168 // variable.
169 i++;
170 continue;
171 }
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500172
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700173 // Metadata variable found, save it and remove it from the set.
174 additionalData.emplace_back(data, length);
175 i = metalist.erase(i);
176 }
177 if (metalist.empty())
178 {
179 // All metadata variables found, break out of journal loop.
180 break;
181 }
182 }
183 if (!metalist.empty())
184 {
185 // Not all the metadata variables were found in the journal.
186 for (auto& metaVarStr : metalist)
187 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500188 lg2::info("Failed to find metadata: {META_FIELD}", "META_FIELD",
189 metaVarStr);
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700190 }
191 }
192
193 sd_journal_close(j);
194 }
Matt Spinlerb60e7552019-07-24 15:28:08 -0500195 createEntry(errMsg, errLvl, additionalData);
196}
197
Patrick Williams597f24a2024-09-27 14:47:42 -0400198auto Manager::createEntry(
199 std::string errMsg, Entry::Level errLvl,
200 std::vector<std::string> additionalData,
201 const FFDCEntries& ffdc) -> sdbusplus::message::object_path
Matt Spinlerb60e7552019-07-24 15:28:08 -0500202{
203 if (!Extensions::disableDefaultLogCaps())
204 {
205 if (errLvl < Entry::sevLowerLimit)
206 {
207 if (realErrors.size() >= ERROR_CAP)
208 {
209 erase(realErrors.front());
210 }
211 }
212 else
213 {
214 if (infoErrors.size() >= ERROR_INFO_CAP)
215 {
216 erase(infoErrors.front());
217 }
218 }
219 }
220
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600221 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500222 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500223 {
224 infoErrors.push_back(entryId);
225 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600226 else
227 {
228 realErrors.push_back(entryId);
229 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600230 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700231 std::chrono::system_clock::now().time_since_epoch())
232 .count();
233 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600234
Patrick Venturef18bf832018-10-26 18:14:00 -0700235 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600236 processMetadata(errMsg, additionalData, objects);
237
Matt Spinlerfb978da2022-01-21 08:42:24 -0600238 auto e = std::make_unique<Entry>(
239 busLog, objPath, entryId,
240 ms, // Milliseconds since 1970
241 errLvl, std::move(errMsg), std::move(additionalData),
242 std::move(objects), fwVersion, getEntrySerializePath(entryId), *this);
243
244 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500245
Matt Spinler601b8112022-01-26 13:18:04 -0600246 if (isQuiesceOnErrorEnabled() && (errLvl < Entry::sevLowerLimit) &&
247 isCalloutPresent(*e))
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500248 {
Andrew Geissler32874542020-07-09 09:17:03 -0500249 quiesceOnError(entryId);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500250 }
251
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600252 // Add entry before calling the extensions so that they have access to it
253 entries.insert(std::make_pair(entryId, std::move(e)));
254
255 doExtensionLogCreate(*entries.find(entryId)->second, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500256
257 // Note: No need to close the file descriptors in the FFDC.
Patrick Williams597f24a2024-09-27 14:47:42 -0400258
259 return objPath;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500260}
261
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500262bool Manager::isQuiesceOnErrorEnabled()
263{
Patrick Williamsa5171972021-04-16 20:10:01 -0500264 // When running under tests, the Logging.Settings service will not be
265 // present. Assume false.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700266 if (IS_UNIT_TEST)
267 {
268 return false;
269 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500270
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500271 std::variant<bool> property;
272
273 auto method = this->busLog.new_method_call(
274 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
275 "org.freedesktop.DBus.Properties", "Get");
276
277 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
278
279 try
280 {
281 auto reply = this->busLog.call(method);
282 reply.read(property);
283 }
Patrick Williams45e83522022-07-22 19:26:52 -0500284 catch (const sdbusplus::exception_t& e)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500285 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500286 lg2::error("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
287 e);
Matt Spinler7ba17c32023-06-12 09:20:15 -0500288 return false;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500289 }
290
291 return std::get<bool>(property);
292}
293
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500294bool Manager::isCalloutPresent(const Entry& entry)
295{
296 for (const auto& c : entry.additionalData())
297 {
298 if (c.find("CALLOUT_") != std::string::npos)
299 {
300 return true;
301 }
302 }
303
304 return false;
305}
306
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500307void Manager::findAndRemoveResolvedBlocks()
308{
309 for (auto& entry : entries)
310 {
311 if (entry.second->resolved())
312 {
313 checkAndRemoveBlockingError(entry.first);
314 }
315 }
316}
317
Patrick Williams45e83522022-07-22 19:26:52 -0500318void Manager::onEntryResolve(sdbusplus::message_t& msg)
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500319{
320 using Interface = std::string;
321 using Property = std::string;
322 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500323 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500324
325 Interface interface;
326 Properties properties;
327
328 msg.read(interface, properties);
329
330 for (const auto& p : properties)
331 {
332 if (p.first == "Resolved")
333 {
334 findAndRemoveResolvedBlocks();
335 return;
336 }
337 }
338}
339
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500340void Manager::checkAndQuiesceHost()
341{
Willy Tu6ddbf692023-09-05 10:54:16 -0700342 using Host = sdbusplus::server::xyz::openbmc_project::state::Host;
Patrick Williamsa144a272021-07-16 17:03:55 -0500343
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500344 // First check host state
Patrick Williamsa144a272021-07-16 17:03:55 -0500345 std::variant<Host::HostState> property;
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500346
347 auto method = this->busLog.new_method_call(
348 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
349 "org.freedesktop.DBus.Properties", "Get");
350
351 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
352
353 try
354 {
355 auto reply = this->busLog.call(method);
356 reply.read(property);
357 }
Patrick Williams45e83522022-07-22 19:26:52 -0500358 catch (const sdbusplus::exception_t& e)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500359 {
360 // Quiescing the host is a "best effort" type function. If unable to
361 // read the host state or it comes back empty, just return.
362 // The boot block object will still be created and the associations to
363 // find the log will be present. Don't want a dependency with
364 // phosphor-state-manager service
Patrick Williams5f285c52021-07-27 21:25:38 -0500365 lg2::info("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
366 e);
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500367 return;
368 }
369
Patrick Williamsa144a272021-07-16 17:03:55 -0500370 auto hostState = std::get<Host::HostState>(property);
371 if (hostState != Host::HostState::Running)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500372 {
373 return;
374 }
375
376 auto quiesce = this->busLog.new_method_call(
377 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
378 "org.freedesktop.systemd1.Manager", "StartUnit");
379
Andrew Geissler86a60e92022-04-15 14:36:02 -0500380 quiesce.append("obmc-host-graceful-quiesce@0.target");
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500381 quiesce.append("replace");
382
383 this->busLog.call_noreply(quiesce);
384}
385
Andrew Geissler32874542020-07-09 09:17:03 -0500386void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500387{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500388 // Verify we don't already have this entry blocking
Matt Spinler4a375952022-07-01 11:15:33 -0500389 auto it = find_if(this->blockingErrors.begin(), this->blockingErrors.end(),
390 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400391 return obj->entryId == entryId;
392 });
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500393 if (it != this->blockingErrors.end())
394 {
395 // Already recorded so just return
Patrick Williams5f285c52021-07-27 21:25:38 -0500396 lg2::debug(
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500397 "QuiesceOnError set and callout present but entry already logged");
398 return;
399 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500400
Patrick Williams5f285c52021-07-27 21:25:38 -0500401 lg2::info("QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500402
Patrick Williams075c7922024-08-16 15:19:49 -0400403 auto blockPath =
404 std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId);
Andrew Geissler32874542020-07-09 09:17:03 -0500405 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500406 this->blockingErrors.push_back(std::move(blockObj));
407
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500408 // Register call back if log is resolved
409 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500410 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Patrick Williams45e83522022-07-22 19:26:52 -0500411 auto callback = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500412 this->busLog,
413 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
414 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
415 std::placeholders::_1));
416
417 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500418 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500419
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500420 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500421}
422
Matt Spinlerc64b7122020-03-26 10:55:01 -0500423void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500424{
425 // Make the association <endpointpath>/<endpointtype> paths
426 std::vector<std::string> assocs;
427 for (const auto& [forwardType, reverseType, endpoint] :
428 entry.associations())
429 {
430 std::string e{endpoint};
431 e += '/' + reverseType;
432 assocs.push_back(e);
433 }
434
435 for (auto& create : Extensions::getCreateFunctions())
436 {
437 try
438 {
439 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500440 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500441 }
Patrick Williams66491c62021-10-06 12:23:37 -0500442 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500443 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500444 lg2::error(
445 "An extension's create function threw an exception: {ERROR}",
446 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500447 }
448 }
449}
450
Patrick Williamsf40323d2021-04-16 15:35:17 -0500451void Manager::processMetadata(const std::string& /*errorName*/,
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600452 const std::vector<std::string>& additionalData,
453 AssociationList& objects) const
454{
455 // additionalData is a list of "metadata=value"
456 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700457 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600458 {
Patrick Venture34438962018-10-30 13:17:37 -0700459 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700460 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600461 {
Patrick Venture34438962018-10-30 13:17:37 -0700462 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600463 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700464 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600465 {
466 (iter->second)(metadata, additionalData, objects);
467 }
468 }
469 }
470}
471
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500472void Manager::checkAndRemoveBlockingError(uint32_t entryId)
473{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500474 // First look for blocking object and remove
Matt Spinler4a375952022-07-01 11:15:33 -0500475 auto it = find_if(blockingErrors.begin(), blockingErrors.end(),
476 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400477 return obj->entryId == entryId;
478 });
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500479 if (it != blockingErrors.end())
480 {
481 blockingErrors.erase(it);
482 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500483
484 // Now remove the callback looking for the error to be resolved
485 auto resolveFind = propChangedEntryCallback.find(entryId);
486 if (resolveFind != propChangedEntryCallback.end())
487 {
488 propChangedEntryCallback.erase(resolveFind);
489 }
490
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500491 return;
492}
493
harsh-agarwal1d763db32024-09-03 09:18:50 -0500494size_t Manager::eraseAll()
495{
496 std::vector<uint32_t> logIDWithHwIsolation;
497 for (auto& func : Extensions::getLogIDWithHwIsolationFunctions())
498 {
499 try
500 {
501 func(logIDWithHwIsolation);
502 }
503 catch (const std::exception& e)
504 {
505 lg2::error("An extension's LogIDWithHwIsolation function threw an "
506 "exception: {ERROR}",
507 "ERROR", e);
508 }
509 }
510 size_t entriesSize = entries.size();
511 auto iter = entries.begin();
512 if (logIDWithHwIsolation.empty())
513 {
514 while (iter != entries.end())
515 {
516 auto e = iter->first;
517 ++iter;
518 erase(e);
519 }
520 entryId = 0;
521 }
522 else
523 {
524 while (iter != entries.end())
525 {
526 auto e = iter->first;
527 ++iter;
528 try
529 {
530 if (!std::ranges::contains(logIDWithHwIsolation, e))
531 {
532 erase(e);
533 }
534 else
535 {
536 entriesSize--;
537 }
538 }
539 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
540 Unavailable& e)
541 {
542 entriesSize--;
543 }
544 }
545 if (!entries.empty())
546 {
547 entryId = std::ranges::max_element(entries, [](const auto& a,
548 const auto& b) {
549 return a.first < b.first;
550 })->first;
551 }
552 else
553 {
554 entryId = 0;
555 }
556 }
557 return entriesSize;
558}
559
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500560void Manager::erase(uint32_t entryId)
561{
Patrick Venture34438962018-10-30 13:17:37 -0700562 auto entryFound = entries.find(entryId);
563 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500564 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500565 for (auto& func : Extensions::getDeleteProhibitedFunctions())
566 {
567 try
568 {
569 bool prohibited = false;
570 func(entryId, prohibited);
571 if (prohibited)
572 {
harsh-agarwal1d763db32024-09-03 09:18:50 -0500573 throw sdbusplus::xyz::openbmc_project::Common::Error::
574 Unavailable();
Matt Spinler99c2b402019-05-23 14:29:16 -0500575 }
576 }
harsh-agarwal1d763db32024-09-03 09:18:50 -0500577 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
578 Unavailable& e)
579 {
580 throw;
581 }
Patrick Williams66491c62021-10-06 12:23:37 -0500582 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500583 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500584 lg2::error("An extension's deleteProhibited function threw an "
585 "exception: {ERROR}",
586 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500587 }
588 }
589
Deepak Kodihalli33887992017-06-13 07:06:49 -0500590 // Delete the persistent representation of this error.
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400591 fs::path errorPath(paths::error());
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600592 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500593 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600594
Patrick Venturef18bf832018-10-26 18:14:00 -0700595 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600596 auto it = std::find(ids.begin(), ids.end(), id);
597 if (it != ids.end())
598 {
599 ids.erase(it);
600 }
601 };
Patrick Venture34438962018-10-30 13:17:37 -0700602 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500603 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600604 removeId(infoErrors, entryId);
605 }
606 else
607 {
608 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500609 }
Patrick Venture34438962018-10-30 13:17:37 -0700610 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500611
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500612 checkAndRemoveBlockingError(entryId);
613
Matt Spinler99c2b402019-05-23 14:29:16 -0500614 for (auto& remove : Extensions::getDeleteFunctions())
615 {
616 try
617 {
618 remove(entryId);
619 }
Patrick Williams66491c62021-10-06 12:23:37 -0500620 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500621 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500622 lg2::error("An extension's delete function threw an exception: "
623 "{ERROR}",
624 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500625 }
626 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500627 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600628 else
629 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500630 lg2::error("Invalid entry ID ({ID}) to delete", "ID", entryId);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600631 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500632}
633
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500634void Manager::restore()
635{
Patrick Venturef18bf832018-10-26 18:14:00 -0700636 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600637 return id == restoredId;
638 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500639
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400640 fs::path dir(paths::error());
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500641 if (!fs::exists(dir) || fs::is_empty(dir))
642 {
643 return;
644 }
645
Patrick Venturef18bf832018-10-26 18:14:00 -0700646 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500647 {
648 auto id = file.path().filename().c_str();
649 auto idNum = std::stol(id);
650 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700651 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500652 if (deserialize(file.path(), *e))
653 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700654 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600655 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500656 {
Matt Spinleref952af2021-08-19 10:23:05 -0500657 e->path(file.path(), true);
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600658 if (e->severity() >= Entry::sevLowerLimit)
659 {
660 infoErrors.push_back(idNum);
661 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600662 else
663 {
664 realErrors.push_back(idNum);
665 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600666
667 entries.insert(std::make_pair(idNum, std::move(e)));
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500668 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600669 else
670 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500671 lg2::error(
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600672 "Failed in sanity check while restoring error entry. "
Patrick Williams5f285c52021-07-27 21:25:38 -0500673 "Ignoring error entry {ID_NUM}/{ENTRY_ID}.",
674 "ID_NUM", idNum, "ENTRY_ID", e->id());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600675 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500676 }
677 }
678
Lei YU01bf5c42022-05-23 15:33:23 +0800679 if (!entries.empty())
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530680 {
Lei YU01bf5c42022-05-23 15:33:23 +0800681 entryId = entries.rbegin()->first;
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530682 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500683}
684
Matt Spinler1275bd12018-05-01 15:13:53 -0500685std::string Manager::readFWVersion()
686{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500687 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500688
Matt Spinlerf61f2922020-06-23 11:32:49 -0500689 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500690 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500691 lg2::error("Unable to read BMC firmware version");
Matt Spinler1275bd12018-05-01 15:13:53 -0500692 }
693
Matt Spinlerf61f2922020-06-23 11:32:49 -0500694 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500695}
696
Patrick Williams597f24a2024-09-27 14:47:42 -0400697auto Manager::create(const std::string& message, Entry::Level severity,
Paul Fertser221b79b2024-03-04 15:40:23 +0000698 const std::map<std::string, std::string>& additionalData,
Patrick Williams597f24a2024-09-27 14:47:42 -0400699 const FFDCEntries& ffdc) -> sdbusplus::message::object_path
Matt Spinlerc64b7122020-03-26 10:55:01 -0500700{
701 // Convert the map into a vector of "key=value" strings
702 std::vector<std::string> ad;
703 metadata::associations::combine(additionalData, ad);
704
Patrick Williams597f24a2024-09-27 14:47:42 -0400705 return createEntry(message, severity, ad, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500706}
707
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500708} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600709} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700710} // namespace phosphor