blob: e4c38d27b93486d19106a9275f0c45aa32b3298e [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 Williams9ca4d132024-10-31 17:02:47 -04009#include "lib/lg2_commit.hpp"
Patrick Williamsfa2d9622024-09-30 16:25:43 -040010#include "paths.hpp"
Matt Spinlerf61f2922020-06-23 11:32:49 -050011#include "util.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -070012
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050013#include <systemd/sd-bus.h>
Adriana Kobylakd311bc82016-10-16 09:54:40 -050014#include <systemd/sd-journal.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050015#include <unistd.h>
Patrick Venturef18bf832018-10-26 18:14:00 -070016
Patrick Williams2544b412022-10-04 08:41:06 -050017#include <phosphor-logging/lg2.hpp>
18#include <sdbusplus/vtable.hpp>
19#include <xyz/openbmc_project/State/Host/server.hpp>
20
Matt Spinler99c2b402019-05-23 14:29:16 -050021#include <cassert>
Patrick Venturef18bf832018-10-26 18:14:00 -070022#include <chrono>
23#include <cstdio>
Patrick Venture30047bf2018-11-01 18:52:15 -070024#include <cstring>
Patrick Venture30047bf2018-11-01 18:52:15 -070025#include <functional>
Patrick Venture30047bf2018-11-01 18:52:15 -070026#include <map>
Patrick Williamsea6d9c42024-12-11 14:58:21 -050027#include <ranges>
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 Williams64a9eaa2024-11-22 19:58:21 -050086 std::map<std::string, std::string> additionalData{};
Patrick Williamsa5171972021-04-16 20:10:01 -050087
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.
Patrick Williams64a9eaa2024-11-22 19:58:21 -0500173 std::string metadata(data, length);
174 if (auto pos = metadata.find('='); pos != std::string::npos)
175 {
176 auto key = metadata.substr(0, pos);
177 auto value = metadata.substr(pos + 1);
178 additionalData.emplace(std::move(key), std::move(value));
179 }
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700180 i = metalist.erase(i);
181 }
182 if (metalist.empty())
183 {
184 // All metadata variables found, break out of journal loop.
185 break;
186 }
187 }
188 if (!metalist.empty())
189 {
190 // Not all the metadata variables were found in the journal.
191 for (auto& metaVarStr : metalist)
192 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500193 lg2::info("Failed to find metadata: {META_FIELD}", "META_FIELD",
194 metaVarStr);
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700195 }
196 }
197
198 sd_journal_close(j);
199 }
Patrick Williams64a9eaa2024-11-22 19:58:21 -0500200 createEntry(errMsg, errLvl, additionalData);
Matt Spinlerb60e7552019-07-24 15:28:08 -0500201}
202
Patrick Williamsb6d3e2f2024-12-18 11:20:17 -0500203auto Manager::createEntry(std::string errMsg, Entry::Level errLvl,
204 std::map<std::string, std::string> additionalData,
205 const FFDCEntries& ffdc)
206 -> sdbusplus::message::object_path
Matt Spinlerb60e7552019-07-24 15:28:08 -0500207{
208 if (!Extensions::disableDefaultLogCaps())
209 {
210 if (errLvl < Entry::sevLowerLimit)
211 {
212 if (realErrors.size() >= ERROR_CAP)
213 {
214 erase(realErrors.front());
215 }
216 }
217 else
218 {
219 if (infoErrors.size() >= ERROR_INFO_CAP)
220 {
221 erase(infoErrors.front());
222 }
223 }
224 }
225
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600226 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500227 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500228 {
229 infoErrors.push_back(entryId);
230 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600231 else
232 {
233 realErrors.push_back(entryId);
234 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600235 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700236 std::chrono::system_clock::now().time_since_epoch())
237 .count();
238 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600239
Patrick Venturef18bf832018-10-26 18:14:00 -0700240 AssociationList objects{};
Patrick Williamsea21d992024-11-22 17:06:35 -0500241 auto additionalDataVec = util::additional_data::combine(additionalData);
242 processMetadata(errMsg, additionalDataVec, objects);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600243
Matt Spinlerfb978da2022-01-21 08:42:24 -0600244 auto e = std::make_unique<Entry>(
245 busLog, objPath, entryId,
246 ms, // Milliseconds since 1970
247 errLvl, std::move(errMsg), std::move(additionalData),
248 std::move(objects), fwVersion, getEntrySerializePath(entryId), *this);
249
250 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500251
Matt Spinler601b8112022-01-26 13:18:04 -0600252 if (isQuiesceOnErrorEnabled() && (errLvl < Entry::sevLowerLimit) &&
253 isCalloutPresent(*e))
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500254 {
Andrew Geissler32874542020-07-09 09:17:03 -0500255 quiesceOnError(entryId);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500256 }
257
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600258 // Add entry before calling the extensions so that they have access to it
259 entries.insert(std::make_pair(entryId, std::move(e)));
260
261 doExtensionLogCreate(*entries.find(entryId)->second, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500262
263 // Note: No need to close the file descriptors in the FFDC.
Patrick Williams597f24a2024-09-27 14:47:42 -0400264
265 return objPath;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500266}
267
Patrick Williams25291152025-02-01 08:21:42 -0500268auto Manager::createFromEvent(
269 sdbusplus::exception::generated_event_base&& event)
270 -> sdbusplus::message::object_path
Patrick Williams9ca4d132024-10-31 17:02:47 -0400271{
272 auto [msg, level, data] = lg2::details::extractEvent(std::move(event));
273 return this->createEntry(msg, level, std::move(data));
274}
275
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500276bool Manager::isQuiesceOnErrorEnabled()
277{
Patrick Williamsa5171972021-04-16 20:10:01 -0500278 // When running under tests, the Logging.Settings service will not be
279 // present. Assume false.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700280 if (IS_UNIT_TEST)
281 {
282 return false;
283 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500284
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500285 std::variant<bool> property;
286
287 auto method = this->busLog.new_method_call(
288 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
289 "org.freedesktop.DBus.Properties", "Get");
290
291 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
292
293 try
294 {
295 auto reply = this->busLog.call(method);
296 reply.read(property);
297 }
Patrick Williams45e83522022-07-22 19:26:52 -0500298 catch (const sdbusplus::exception_t& e)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500299 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500300 lg2::error("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
301 e);
Matt Spinler7ba17c32023-06-12 09:20:15 -0500302 return false;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500303 }
304
305 return std::get<bool>(property);
306}
307
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500308bool Manager::isCalloutPresent(const Entry& entry)
309{
Patrick Williamsea6d9c42024-12-11 14:58:21 -0500310 for (const auto& c : std::views::keys(entry.additionalData()))
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500311 {
312 if (c.find("CALLOUT_") != std::string::npos)
313 {
314 return true;
315 }
316 }
317
318 return false;
319}
320
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500321void Manager::findAndRemoveResolvedBlocks()
322{
323 for (auto& entry : entries)
324 {
325 if (entry.second->resolved())
326 {
327 checkAndRemoveBlockingError(entry.first);
328 }
329 }
330}
331
Patrick Williams45e83522022-07-22 19:26:52 -0500332void Manager::onEntryResolve(sdbusplus::message_t& msg)
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500333{
334 using Interface = std::string;
335 using Property = std::string;
336 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500337 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500338
339 Interface interface;
340 Properties properties;
341
342 msg.read(interface, properties);
343
344 for (const auto& p : properties)
345 {
346 if (p.first == "Resolved")
347 {
348 findAndRemoveResolvedBlocks();
349 return;
350 }
351 }
352}
353
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500354void Manager::checkAndQuiesceHost()
355{
Willy Tu6ddbf692023-09-05 10:54:16 -0700356 using Host = sdbusplus::server::xyz::openbmc_project::state::Host;
Patrick Williamsa144a272021-07-16 17:03:55 -0500357
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500358 // First check host state
Patrick Williamsa144a272021-07-16 17:03:55 -0500359 std::variant<Host::HostState> property;
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500360
361 auto method = this->busLog.new_method_call(
362 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
363 "org.freedesktop.DBus.Properties", "Get");
364
365 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
366
367 try
368 {
369 auto reply = this->busLog.call(method);
370 reply.read(property);
371 }
Patrick Williams45e83522022-07-22 19:26:52 -0500372 catch (const sdbusplus::exception_t& e)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500373 {
374 // Quiescing the host is a "best effort" type function. If unable to
375 // read the host state or it comes back empty, just return.
376 // The boot block object will still be created and the associations to
377 // find the log will be present. Don't want a dependency with
378 // phosphor-state-manager service
Patrick Williams5f285c52021-07-27 21:25:38 -0500379 lg2::info("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
380 e);
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500381 return;
382 }
383
Patrick Williamsa144a272021-07-16 17:03:55 -0500384 auto hostState = std::get<Host::HostState>(property);
385 if (hostState != Host::HostState::Running)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500386 {
387 return;
388 }
389
390 auto quiesce = this->busLog.new_method_call(
391 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
392 "org.freedesktop.systemd1.Manager", "StartUnit");
393
Andrew Geissler86a60e92022-04-15 14:36:02 -0500394 quiesce.append("obmc-host-graceful-quiesce@0.target");
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500395 quiesce.append("replace");
396
397 this->busLog.call_noreply(quiesce);
398}
399
Andrew Geissler32874542020-07-09 09:17:03 -0500400void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500401{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500402 // Verify we don't already have this entry blocking
Matt Spinler4a375952022-07-01 11:15:33 -0500403 auto it = find_if(this->blockingErrors.begin(), this->blockingErrors.end(),
404 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400405 return obj->entryId == entryId;
406 });
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500407 if (it != this->blockingErrors.end())
408 {
409 // Already recorded so just return
Patrick Williams5f285c52021-07-27 21:25:38 -0500410 lg2::debug(
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500411 "QuiesceOnError set and callout present but entry already logged");
412 return;
413 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500414
Patrick Williams5f285c52021-07-27 21:25:38 -0500415 lg2::info("QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500416
Patrick Williams075c7922024-08-16 15:19:49 -0400417 auto blockPath =
418 std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId);
Andrew Geissler32874542020-07-09 09:17:03 -0500419 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500420 this->blockingErrors.push_back(std::move(blockObj));
421
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500422 // Register call back if log is resolved
423 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500424 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Patrick Williams45e83522022-07-22 19:26:52 -0500425 auto callback = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500426 this->busLog,
427 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
428 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
429 std::placeholders::_1));
430
431 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500432 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500433
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500434 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500435}
436
Matt Spinlerc64b7122020-03-26 10:55:01 -0500437void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500438{
439 // Make the association <endpointpath>/<endpointtype> paths
440 std::vector<std::string> assocs;
441 for (const auto& [forwardType, reverseType, endpoint] :
442 entry.associations())
443 {
444 std::string e{endpoint};
445 e += '/' + reverseType;
446 assocs.push_back(e);
447 }
448
449 for (auto& create : Extensions::getCreateFunctions())
450 {
451 try
452 {
453 create(entry.message(), entry.id(), entry.timestamp(),
Patrick Williams109b4a52025-03-20 20:19:36 -0400454 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500455 }
Patrick Williams66491c62021-10-06 12:23:37 -0500456 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500457 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500458 lg2::error(
459 "An extension's create function threw an exception: {ERROR}",
460 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500461 }
462 }
463}
464
Patrick Williamsf40323d2021-04-16 15:35:17 -0500465void Manager::processMetadata(const std::string& /*errorName*/,
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600466 const std::vector<std::string>& additionalData,
467 AssociationList& objects) const
468{
469 // additionalData is a list of "metadata=value"
470 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700471 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600472 {
Patrick Venture34438962018-10-30 13:17:37 -0700473 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700474 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600475 {
Patrick Venture34438962018-10-30 13:17:37 -0700476 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600477 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700478 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600479 {
480 (iter->second)(metadata, additionalData, objects);
481 }
482 }
483 }
484}
485
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500486void Manager::checkAndRemoveBlockingError(uint32_t entryId)
487{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500488 // First look for blocking object and remove
Matt Spinler4a375952022-07-01 11:15:33 -0500489 auto it = find_if(blockingErrors.begin(), blockingErrors.end(),
490 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400491 return obj->entryId == entryId;
492 });
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500493 if (it != blockingErrors.end())
494 {
495 blockingErrors.erase(it);
496 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500497
498 // Now remove the callback looking for the error to be resolved
499 auto resolveFind = propChangedEntryCallback.find(entryId);
500 if (resolveFind != propChangedEntryCallback.end())
501 {
502 propChangedEntryCallback.erase(resolveFind);
503 }
504
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500505 return;
506}
507
harsh-agarwal1d763db32024-09-03 09:18:50 -0500508size_t Manager::eraseAll()
509{
510 std::vector<uint32_t> logIDWithHwIsolation;
511 for (auto& func : Extensions::getLogIDWithHwIsolationFunctions())
512 {
513 try
514 {
515 func(logIDWithHwIsolation);
516 }
517 catch (const std::exception& e)
518 {
519 lg2::error("An extension's LogIDWithHwIsolation function threw an "
520 "exception: {ERROR}",
521 "ERROR", e);
522 }
523 }
524 size_t entriesSize = entries.size();
525 auto iter = entries.begin();
526 if (logIDWithHwIsolation.empty())
527 {
528 while (iter != entries.end())
529 {
530 auto e = iter->first;
531 ++iter;
532 erase(e);
533 }
534 entryId = 0;
535 }
536 else
537 {
538 while (iter != entries.end())
539 {
540 auto e = iter->first;
541 ++iter;
542 try
543 {
544 if (!std::ranges::contains(logIDWithHwIsolation, e))
545 {
546 erase(e);
547 }
548 else
549 {
550 entriesSize--;
551 }
552 }
553 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
554 Unavailable& e)
555 {
556 entriesSize--;
557 }
558 }
559 if (!entries.empty())
560 {
561 entryId = std::ranges::max_element(entries, [](const auto& a,
562 const auto& b) {
563 return a.first < b.first;
564 })->first;
565 }
566 else
567 {
568 entryId = 0;
569 }
570 }
571 return entriesSize;
572}
573
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500574void Manager::erase(uint32_t entryId)
575{
Patrick Venture34438962018-10-30 13:17:37 -0700576 auto entryFound = entries.find(entryId);
577 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500578 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500579 for (auto& func : Extensions::getDeleteProhibitedFunctions())
580 {
581 try
582 {
583 bool prohibited = false;
584 func(entryId, prohibited);
585 if (prohibited)
586 {
harsh-agarwal1d763db32024-09-03 09:18:50 -0500587 throw sdbusplus::xyz::openbmc_project::Common::Error::
588 Unavailable();
Matt Spinler99c2b402019-05-23 14:29:16 -0500589 }
590 }
harsh-agarwal1d763db32024-09-03 09:18:50 -0500591 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
592 Unavailable& e)
593 {
594 throw;
595 }
Patrick Williams66491c62021-10-06 12:23:37 -0500596 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500597 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500598 lg2::error("An extension's deleteProhibited function threw an "
599 "exception: {ERROR}",
600 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500601 }
602 }
603
Deepak Kodihalli33887992017-06-13 07:06:49 -0500604 // Delete the persistent representation of this error.
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400605 fs::path errorPath(paths::error());
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600606 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500607 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600608
Patrick Venturef18bf832018-10-26 18:14:00 -0700609 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600610 auto it = std::find(ids.begin(), ids.end(), id);
611 if (it != ids.end())
612 {
613 ids.erase(it);
614 }
615 };
Patrick Venture34438962018-10-30 13:17:37 -0700616 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500617 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600618 removeId(infoErrors, entryId);
619 }
620 else
621 {
622 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500623 }
Patrick Venture34438962018-10-30 13:17:37 -0700624 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500625
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500626 checkAndRemoveBlockingError(entryId);
627
Matt Spinler99c2b402019-05-23 14:29:16 -0500628 for (auto& remove : Extensions::getDeleteFunctions())
629 {
630 try
631 {
632 remove(entryId);
633 }
Patrick Williams66491c62021-10-06 12:23:37 -0500634 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500635 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500636 lg2::error("An extension's delete function threw an exception: "
637 "{ERROR}",
638 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500639 }
640 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500641 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600642 else
643 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500644 lg2::error("Invalid entry ID ({ID}) to delete", "ID", entryId);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600645 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500646}
647
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500648void Manager::restore()
649{
Patrick Venturef18bf832018-10-26 18:14:00 -0700650 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600651 return id == restoredId;
652 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500653
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400654 fs::path dir(paths::error());
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500655 if (!fs::exists(dir) || fs::is_empty(dir))
656 {
657 return;
658 }
659
Patrick Venturef18bf832018-10-26 18:14:00 -0700660 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500661 {
662 auto id = file.path().filename().c_str();
663 auto idNum = std::stol(id);
664 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700665 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500666 if (deserialize(file.path(), *e))
667 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700668 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600669 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500670 {
Matt Spinleref952af2021-08-19 10:23:05 -0500671 e->path(file.path(), true);
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600672 if (e->severity() >= Entry::sevLowerLimit)
673 {
674 infoErrors.push_back(idNum);
675 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600676 else
677 {
678 realErrors.push_back(idNum);
679 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600680
681 entries.insert(std::make_pair(idNum, std::move(e)));
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500682 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600683 else
684 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500685 lg2::error(
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600686 "Failed in sanity check while restoring error entry. "
Patrick Williams5f285c52021-07-27 21:25:38 -0500687 "Ignoring error entry {ID_NUM}/{ENTRY_ID}.",
688 "ID_NUM", idNum, "ENTRY_ID", e->id());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600689 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500690 }
691 }
692
Lei YU01bf5c42022-05-23 15:33:23 +0800693 if (!entries.empty())
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530694 {
Lei YU01bf5c42022-05-23 15:33:23 +0800695 entryId = entries.rbegin()->first;
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530696 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500697}
698
Matt Spinler1275bd12018-05-01 15:13:53 -0500699std::string Manager::readFWVersion()
700{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500701 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500702
Matt Spinlerf61f2922020-06-23 11:32:49 -0500703 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500704 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500705 lg2::error("Unable to read BMC firmware version");
Matt Spinler1275bd12018-05-01 15:13:53 -0500706 }
707
Matt Spinlerf61f2922020-06-23 11:32:49 -0500708 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500709}
710
Patrick Williams597f24a2024-09-27 14:47:42 -0400711auto Manager::create(const std::string& message, Entry::Level severity,
Paul Fertser221b79b2024-03-04 15:40:23 +0000712 const std::map<std::string, std::string>& additionalData,
Patrick Williams597f24a2024-09-27 14:47:42 -0400713 const FFDCEntries& ffdc) -> sdbusplus::message::object_path
Matt Spinlerc64b7122020-03-26 10:55:01 -0500714{
Patrick Williamsea21d992024-11-22 17:06:35 -0500715 return createEntry(message, severity, additionalData, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500716}
717
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500718} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600719} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700720} // namespace phosphor