blob: b20443f638cb75bec32a9254acbaf4089b72972d [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 Venturef18bf832018-10-26 18:14:00 -070025#include <fstream>
Patrick Venture30047bf2018-11-01 18:52:15 -070026#include <functional>
Patrick Venturef18bf832018-10-26 18:14:00 -070027#include <future>
28#include <iostream>
Patrick Venture30047bf2018-11-01 18:52:15 -070029#include <map>
Patrick Williamsea6d9c42024-12-11 14:58:21 -050030#include <ranges>
Patrick Venturef18bf832018-10-26 18:14:00 -070031#include <set>
32#include <string>
Patrick Williamsb01a5b42021-08-28 15:11:45 -050033#include <string_view>
Patrick Venturef18bf832018-10-26 18:14:00 -070034#include <vector>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060035
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050036using namespace std::chrono;
Patrick Williams5f285c52021-07-27 21:25:38 -050037extern const std::map<
38 phosphor::logging::metadata::Metadata,
39 std::function<phosphor::logging::metadata::associations::Type>>
Patrick Venturef18bf832018-10-26 18:14:00 -070040 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050041
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060042namespace phosphor
43{
44namespace logging
45{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050046namespace internal
47{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050048inline auto getLevel(const std::string& errMsg)
49{
50 auto reqLevel = Entry::Level::Error; // Default to Error
51
52 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050053 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050054 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050055 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050056 }
57
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050058 return reqLevel;
59}
60
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050061int Manager::getRealErrSize()
62{
63 return realErrors.size();
64}
65
66int Manager::getInfoErrSize()
67{
68 return infoErrors.size();
69}
70
Lei YUb50c7052021-01-21 16:02:26 +080071uint32_t Manager::commit(uint64_t transactionId, std::string errMsg)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050072{
73 auto level = getLevel(errMsg);
74 _commit(transactionId, std::move(errMsg), level);
Lei YUb50c7052021-01-21 16:02:26 +080075 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050076}
77
Lei YUb50c7052021-01-21 16:02:26 +080078uint32_t Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
79 uint32_t errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050080{
81 _commit(transactionId, std::move(errMsg),
82 static_cast<Entry::Level>(errLvl));
Lei YUb50c7052021-01-21 16:02:26 +080083 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050084}
85
Patrick Williamsa5171972021-04-16 20:10:01 -050086void Manager::_commit(uint64_t transactionId [[maybe_unused]],
87 std::string&& errMsg, Entry::Level errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050088{
Patrick Williams64a9eaa2024-11-22 19:58:21 -050089 std::map<std::string, std::string> additionalData{};
Patrick Williamsa5171972021-04-16 20:10:01 -050090
91 // When running as a test-case, the system may have a LOT of journal
92 // data and we may not have permissions to do some of the journal sync
93 // operations. Just skip over them.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070094 if (!IS_UNIT_TEST)
Adriana Kobylakd311bc82016-10-16 09:54:40 -050095 {
Patrick Williamsb01a5b42021-08-28 15:11:45 -050096 static constexpr auto transactionIdVar =
97 std::string_view{"TRANSACTION_ID"};
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 transactionIdVarSize = transactionIdVar.size();
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700100 // Length of 'TRANSACTION_ID=' string.
Patrick Williamsb01a5b42021-08-28 15:11:45 -0500101 static constexpr auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500102
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700103 // Flush all the pending log messages into the journal
Matt Spinler271d1432023-01-18 13:58:05 -0600104 util::journalSync();
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600105
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700106 sd_journal* j = nullptr;
107 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600108 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600109 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500110 lg2::error("Failed to open journal: {ERROR}", "ERROR",
111 strerror(-rc));
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700112 return;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600113 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600114
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700115 std::string transactionIdStr = std::to_string(transactionId);
116 std::set<std::string> metalist;
117 auto metamap = g_errMetaMap.find(errMsg);
118 if (metamap != g_errMetaMap.end())
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600119 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700120 metalist.insert(metamap->second.begin(), metamap->second.end());
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600121 }
122
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700123 // Add _PID field information in AdditionalData.
124 metalist.insert("_PID");
125
126 // Read the journal from the end to get the most recent entry first.
127 // The result from the sd_journal_get_data() is of the form
128 // VARIABLE=value.
129 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600130 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700131 const char* data = nullptr;
132 size_t length = 0;
133
134 // Look for the transaction id metadata variable
Patrick Williamsb01a5b42021-08-28 15:11:45 -0500135 rc = sd_journal_get_data(j, transactionIdVar.data(),
136 (const void**)&data, &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600137 if (rc < 0)
138 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700139 // This journal entry does not have the TRANSACTION_ID
140 // metadata variable.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600141 continue;
142 }
143
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700144 // journald does not guarantee that sd_journal_get_data() returns
145 // NULL terminated strings, so need to specify the size to use to
146 // compare, use the returned length instead of anything that relies
147 // on NULL terminators like strlen(). The data variable is in the
148 // form of 'TRANSACTION_ID=1234'. Remove the TRANSACTION_ID
149 // characters plus the (=) sign to do the comparison. 'data +
150 // transactionIdVarOffset' will be in the form of '1234'. 'length -
151 // transactionIdVarOffset' will be the length of '1234'.
152 if ((length <= (transactionIdVarOffset)) ||
153 (transactionIdStr.compare(
154 0, transactionIdStr.size(), data + transactionIdVarOffset,
155 length - transactionIdVarOffset) != 0))
156 {
157 // The value of the TRANSACTION_ID metadata is not the requested
158 // transaction id number.
159 continue;
160 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600161
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700162 // Search for all metadata variables in the current journal entry.
163 for (auto i = metalist.cbegin(); i != metalist.cend();)
164 {
165 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
166 &length);
167 if (rc < 0)
168 {
169 // Metadata variable not found, check next metadata
170 // variable.
171 i++;
172 continue;
173 }
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500174
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700175 // Metadata variable found, save it and remove it from the set.
Patrick Williams64a9eaa2024-11-22 19:58:21 -0500176 std::string metadata(data, length);
177 if (auto pos = metadata.find('='); pos != std::string::npos)
178 {
179 auto key = metadata.substr(0, pos);
180 auto value = metadata.substr(pos + 1);
181 additionalData.emplace(std::move(key), std::move(value));
182 }
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700183 i = metalist.erase(i);
184 }
185 if (metalist.empty())
186 {
187 // All metadata variables found, break out of journal loop.
188 break;
189 }
190 }
191 if (!metalist.empty())
192 {
193 // Not all the metadata variables were found in the journal.
194 for (auto& metaVarStr : metalist)
195 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500196 lg2::info("Failed to find metadata: {META_FIELD}", "META_FIELD",
197 metaVarStr);
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700198 }
199 }
200
201 sd_journal_close(j);
202 }
Patrick Williams64a9eaa2024-11-22 19:58:21 -0500203 createEntry(errMsg, errLvl, additionalData);
Matt Spinlerb60e7552019-07-24 15:28:08 -0500204}
205
Patrick Williams597f24a2024-09-27 14:47:42 -0400206auto Manager::createEntry(
207 std::string errMsg, Entry::Level errLvl,
Patrick Williamsea21d992024-11-22 17:06:35 -0500208 std::map<std::string, std::string> additionalData,
Patrick Williams597f24a2024-09-27 14:47:42 -0400209 const FFDCEntries& ffdc) -> sdbusplus::message::object_path
Matt Spinlerb60e7552019-07-24 15:28:08 -0500210{
211 if (!Extensions::disableDefaultLogCaps())
212 {
213 if (errLvl < Entry::sevLowerLimit)
214 {
215 if (realErrors.size() >= ERROR_CAP)
216 {
217 erase(realErrors.front());
218 }
219 }
220 else
221 {
222 if (infoErrors.size() >= ERROR_INFO_CAP)
223 {
224 erase(infoErrors.front());
225 }
226 }
227 }
228
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600229 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500230 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500231 {
232 infoErrors.push_back(entryId);
233 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600234 else
235 {
236 realErrors.push_back(entryId);
237 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600238 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700239 std::chrono::system_clock::now().time_since_epoch())
240 .count();
241 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600242
Patrick Venturef18bf832018-10-26 18:14:00 -0700243 AssociationList objects{};
Patrick Williamsea21d992024-11-22 17:06:35 -0500244 auto additionalDataVec = util::additional_data::combine(additionalData);
245 processMetadata(errMsg, additionalDataVec, objects);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600246
Matt Spinlerfb978da2022-01-21 08:42:24 -0600247 auto e = std::make_unique<Entry>(
248 busLog, objPath, entryId,
249 ms, // Milliseconds since 1970
250 errLvl, std::move(errMsg), std::move(additionalData),
251 std::move(objects), fwVersion, getEntrySerializePath(entryId), *this);
252
253 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500254
Matt Spinler601b8112022-01-26 13:18:04 -0600255 if (isQuiesceOnErrorEnabled() && (errLvl < Entry::sevLowerLimit) &&
256 isCalloutPresent(*e))
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500257 {
Andrew Geissler32874542020-07-09 09:17:03 -0500258 quiesceOnError(entryId);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500259 }
260
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600261 // Add entry before calling the extensions so that they have access to it
262 entries.insert(std::make_pair(entryId, std::move(e)));
263
264 doExtensionLogCreate(*entries.find(entryId)->second, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500265
266 // Note: No need to close the file descriptors in the FFDC.
Patrick Williams597f24a2024-09-27 14:47:42 -0400267
268 return objPath;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500269}
270
Patrick Williams9ca4d132024-10-31 17:02:47 -0400271auto Manager::createFromEvent(sdbusplus::exception::generated_event_base&&
272 event) -> sdbusplus::message::object_path
273{
274 auto [msg, level, data] = lg2::details::extractEvent(std::move(event));
275 return this->createEntry(msg, level, std::move(data));
276}
277
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500278bool Manager::isQuiesceOnErrorEnabled()
279{
Patrick Williamsa5171972021-04-16 20:10:01 -0500280 // When running under tests, the Logging.Settings service will not be
281 // present. Assume false.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700282 if (IS_UNIT_TEST)
283 {
284 return false;
285 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500286
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500287 std::variant<bool> property;
288
289 auto method = this->busLog.new_method_call(
290 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
291 "org.freedesktop.DBus.Properties", "Get");
292
293 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
294
295 try
296 {
297 auto reply = this->busLog.call(method);
298 reply.read(property);
299 }
Patrick Williams45e83522022-07-22 19:26:52 -0500300 catch (const sdbusplus::exception_t& e)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500301 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500302 lg2::error("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
303 e);
Matt Spinler7ba17c32023-06-12 09:20:15 -0500304 return false;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500305 }
306
307 return std::get<bool>(property);
308}
309
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500310bool Manager::isCalloutPresent(const Entry& entry)
311{
Patrick Williamsea6d9c42024-12-11 14:58:21 -0500312 for (const auto& c : std::views::keys(entry.additionalData()))
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500313 {
314 if (c.find("CALLOUT_") != std::string::npos)
315 {
316 return true;
317 }
318 }
319
320 return false;
321}
322
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500323void Manager::findAndRemoveResolvedBlocks()
324{
325 for (auto& entry : entries)
326 {
327 if (entry.second->resolved())
328 {
329 checkAndRemoveBlockingError(entry.first);
330 }
331 }
332}
333
Patrick Williams45e83522022-07-22 19:26:52 -0500334void Manager::onEntryResolve(sdbusplus::message_t& msg)
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500335{
336 using Interface = std::string;
337 using Property = std::string;
338 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500339 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500340
341 Interface interface;
342 Properties properties;
343
344 msg.read(interface, properties);
345
346 for (const auto& p : properties)
347 {
348 if (p.first == "Resolved")
349 {
350 findAndRemoveResolvedBlocks();
351 return;
352 }
353 }
354}
355
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500356void Manager::checkAndQuiesceHost()
357{
Willy Tu6ddbf692023-09-05 10:54:16 -0700358 using Host = sdbusplus::server::xyz::openbmc_project::state::Host;
Patrick Williamsa144a272021-07-16 17:03:55 -0500359
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500360 // First check host state
Patrick Williamsa144a272021-07-16 17:03:55 -0500361 std::variant<Host::HostState> property;
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500362
363 auto method = this->busLog.new_method_call(
364 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
365 "org.freedesktop.DBus.Properties", "Get");
366
367 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
368
369 try
370 {
371 auto reply = this->busLog.call(method);
372 reply.read(property);
373 }
Patrick Williams45e83522022-07-22 19:26:52 -0500374 catch (const sdbusplus::exception_t& e)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500375 {
376 // Quiescing the host is a "best effort" type function. If unable to
377 // read the host state or it comes back empty, just return.
378 // The boot block object will still be created and the associations to
379 // find the log will be present. Don't want a dependency with
380 // phosphor-state-manager service
Patrick Williams5f285c52021-07-27 21:25:38 -0500381 lg2::info("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
382 e);
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500383 return;
384 }
385
Patrick Williamsa144a272021-07-16 17:03:55 -0500386 auto hostState = std::get<Host::HostState>(property);
387 if (hostState != Host::HostState::Running)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500388 {
389 return;
390 }
391
392 auto quiesce = this->busLog.new_method_call(
393 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
394 "org.freedesktop.systemd1.Manager", "StartUnit");
395
Andrew Geissler86a60e92022-04-15 14:36:02 -0500396 quiesce.append("obmc-host-graceful-quiesce@0.target");
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500397 quiesce.append("replace");
398
399 this->busLog.call_noreply(quiesce);
400}
401
Andrew Geissler32874542020-07-09 09:17:03 -0500402void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500403{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500404 // Verify we don't already have this entry blocking
Matt Spinler4a375952022-07-01 11:15:33 -0500405 auto it = find_if(this->blockingErrors.begin(), this->blockingErrors.end(),
406 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400407 return obj->entryId == entryId;
408 });
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500409 if (it != this->blockingErrors.end())
410 {
411 // Already recorded so just return
Patrick Williams5f285c52021-07-27 21:25:38 -0500412 lg2::debug(
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500413 "QuiesceOnError set and callout present but entry already logged");
414 return;
415 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500416
Patrick Williams5f285c52021-07-27 21:25:38 -0500417 lg2::info("QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500418
Patrick Williams075c7922024-08-16 15:19:49 -0400419 auto blockPath =
420 std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId);
Andrew Geissler32874542020-07-09 09:17:03 -0500421 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500422 this->blockingErrors.push_back(std::move(blockObj));
423
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500424 // Register call back if log is resolved
425 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500426 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Patrick Williams45e83522022-07-22 19:26:52 -0500427 auto callback = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500428 this->busLog,
429 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
430 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
431 std::placeholders::_1));
432
433 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500434 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500435
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500436 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500437}
438
Matt Spinlerc64b7122020-03-26 10:55:01 -0500439void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500440{
441 // Make the association <endpointpath>/<endpointtype> paths
442 std::vector<std::string> assocs;
443 for (const auto& [forwardType, reverseType, endpoint] :
444 entry.associations())
445 {
446 std::string e{endpoint};
447 e += '/' + reverseType;
448 assocs.push_back(e);
449 }
450
451 for (auto& create : Extensions::getCreateFunctions())
452 {
453 try
454 {
455 create(entry.message(), entry.id(), entry.timestamp(),
Patrick Williamse5940632024-11-22 20:47:58 -0500456 entry.severity(), entry.additionalData2(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500457 }
Patrick Williams66491c62021-10-06 12:23:37 -0500458 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500459 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500460 lg2::error(
461 "An extension's create function threw an exception: {ERROR}",
462 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500463 }
464 }
465}
466
Patrick Williamsf40323d2021-04-16 15:35:17 -0500467void Manager::processMetadata(const std::string& /*errorName*/,
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600468 const std::vector<std::string>& additionalData,
469 AssociationList& objects) const
470{
471 // additionalData is a list of "metadata=value"
472 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700473 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600474 {
Patrick Venture34438962018-10-30 13:17:37 -0700475 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700476 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600477 {
Patrick Venture34438962018-10-30 13:17:37 -0700478 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600479 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700480 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600481 {
482 (iter->second)(metadata, additionalData, objects);
483 }
484 }
485 }
486}
487
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500488void Manager::checkAndRemoveBlockingError(uint32_t entryId)
489{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500490 // First look for blocking object and remove
Matt Spinler4a375952022-07-01 11:15:33 -0500491 auto it = find_if(blockingErrors.begin(), blockingErrors.end(),
492 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400493 return obj->entryId == entryId;
494 });
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500495 if (it != blockingErrors.end())
496 {
497 blockingErrors.erase(it);
498 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500499
500 // Now remove the callback looking for the error to be resolved
501 auto resolveFind = propChangedEntryCallback.find(entryId);
502 if (resolveFind != propChangedEntryCallback.end())
503 {
504 propChangedEntryCallback.erase(resolveFind);
505 }
506
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500507 return;
508}
509
harsh-agarwal1d763db32024-09-03 09:18:50 -0500510size_t Manager::eraseAll()
511{
512 std::vector<uint32_t> logIDWithHwIsolation;
513 for (auto& func : Extensions::getLogIDWithHwIsolationFunctions())
514 {
515 try
516 {
517 func(logIDWithHwIsolation);
518 }
519 catch (const std::exception& e)
520 {
521 lg2::error("An extension's LogIDWithHwIsolation function threw an "
522 "exception: {ERROR}",
523 "ERROR", e);
524 }
525 }
526 size_t entriesSize = entries.size();
527 auto iter = entries.begin();
528 if (logIDWithHwIsolation.empty())
529 {
530 while (iter != entries.end())
531 {
532 auto e = iter->first;
533 ++iter;
534 erase(e);
535 }
536 entryId = 0;
537 }
538 else
539 {
540 while (iter != entries.end())
541 {
542 auto e = iter->first;
543 ++iter;
544 try
545 {
546 if (!std::ranges::contains(logIDWithHwIsolation, e))
547 {
548 erase(e);
549 }
550 else
551 {
552 entriesSize--;
553 }
554 }
555 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
556 Unavailable& e)
557 {
558 entriesSize--;
559 }
560 }
561 if (!entries.empty())
562 {
563 entryId = std::ranges::max_element(entries, [](const auto& a,
564 const auto& b) {
565 return a.first < b.first;
566 })->first;
567 }
568 else
569 {
570 entryId = 0;
571 }
572 }
573 return entriesSize;
574}
575
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500576void Manager::erase(uint32_t entryId)
577{
Patrick Venture34438962018-10-30 13:17:37 -0700578 auto entryFound = entries.find(entryId);
579 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500580 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500581 for (auto& func : Extensions::getDeleteProhibitedFunctions())
582 {
583 try
584 {
585 bool prohibited = false;
586 func(entryId, prohibited);
587 if (prohibited)
588 {
harsh-agarwal1d763db32024-09-03 09:18:50 -0500589 throw sdbusplus::xyz::openbmc_project::Common::Error::
590 Unavailable();
Matt Spinler99c2b402019-05-23 14:29:16 -0500591 }
592 }
harsh-agarwal1d763db32024-09-03 09:18:50 -0500593 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
594 Unavailable& e)
595 {
596 throw;
597 }
Patrick Williams66491c62021-10-06 12:23:37 -0500598 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500599 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500600 lg2::error("An extension's deleteProhibited function threw an "
601 "exception: {ERROR}",
602 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500603 }
604 }
605
Deepak Kodihalli33887992017-06-13 07:06:49 -0500606 // Delete the persistent representation of this error.
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400607 fs::path errorPath(paths::error());
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600608 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500609 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600610
Patrick Venturef18bf832018-10-26 18:14:00 -0700611 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600612 auto it = std::find(ids.begin(), ids.end(), id);
613 if (it != ids.end())
614 {
615 ids.erase(it);
616 }
617 };
Patrick Venture34438962018-10-30 13:17:37 -0700618 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500619 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600620 removeId(infoErrors, entryId);
621 }
622 else
623 {
624 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500625 }
Patrick Venture34438962018-10-30 13:17:37 -0700626 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500627
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500628 checkAndRemoveBlockingError(entryId);
629
Matt Spinler99c2b402019-05-23 14:29:16 -0500630 for (auto& remove : Extensions::getDeleteFunctions())
631 {
632 try
633 {
634 remove(entryId);
635 }
Patrick Williams66491c62021-10-06 12:23:37 -0500636 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500637 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500638 lg2::error("An extension's delete function threw an exception: "
639 "{ERROR}",
640 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500641 }
642 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500643 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600644 else
645 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500646 lg2::error("Invalid entry ID ({ID}) to delete", "ID", entryId);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600647 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500648}
649
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500650void Manager::restore()
651{
Patrick Venturef18bf832018-10-26 18:14:00 -0700652 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600653 return id == restoredId;
654 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500655
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400656 fs::path dir(paths::error());
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500657 if (!fs::exists(dir) || fs::is_empty(dir))
658 {
659 return;
660 }
661
Patrick Venturef18bf832018-10-26 18:14:00 -0700662 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500663 {
664 auto id = file.path().filename().c_str();
665 auto idNum = std::stol(id);
666 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700667 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500668 if (deserialize(file.path(), *e))
669 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700670 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600671 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500672 {
Matt Spinleref952af2021-08-19 10:23:05 -0500673 e->path(file.path(), true);
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600674 if (e->severity() >= Entry::sevLowerLimit)
675 {
676 infoErrors.push_back(idNum);
677 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600678 else
679 {
680 realErrors.push_back(idNum);
681 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600682
683 entries.insert(std::make_pair(idNum, std::move(e)));
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500684 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600685 else
686 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500687 lg2::error(
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600688 "Failed in sanity check while restoring error entry. "
Patrick Williams5f285c52021-07-27 21:25:38 -0500689 "Ignoring error entry {ID_NUM}/{ENTRY_ID}.",
690 "ID_NUM", idNum, "ENTRY_ID", e->id());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600691 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500692 }
693 }
694
Lei YU01bf5c42022-05-23 15:33:23 +0800695 if (!entries.empty())
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530696 {
Lei YU01bf5c42022-05-23 15:33:23 +0800697 entryId = entries.rbegin()->first;
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530698 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500699}
700
Matt Spinler1275bd12018-05-01 15:13:53 -0500701std::string Manager::readFWVersion()
702{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500703 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500704
Matt Spinlerf61f2922020-06-23 11:32:49 -0500705 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500706 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500707 lg2::error("Unable to read BMC firmware version");
Matt Spinler1275bd12018-05-01 15:13:53 -0500708 }
709
Matt Spinlerf61f2922020-06-23 11:32:49 -0500710 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500711}
712
Patrick Williams597f24a2024-09-27 14:47:42 -0400713auto Manager::create(const std::string& message, Entry::Level severity,
Paul Fertser221b79b2024-03-04 15:40:23 +0000714 const std::map<std::string, std::string>& additionalData,
Patrick Williams597f24a2024-09-27 14:47:42 -0400715 const FFDCEntries& ffdc) -> sdbusplus::message::object_path
Matt Spinlerc64b7122020-03-26 10:55:01 -0500716{
Patrick Williamsea21d992024-11-22 17:06:35 -0500717 return createEntry(message, severity, additionalData, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500718}
719
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500720} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600721} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700722} // namespace phosphor