blob: a7f44942eaa2daaa19801f1b7943e081018af0fa [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 Williamsb6d3e2f2024-12-18 11:20:17 -0500206auto Manager::createEntry(std::string errMsg, Entry::Level errLvl,
207 std::map<std::string, std::string> additionalData,
208 const FFDCEntries& ffdc)
209 -> 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 Williamsb6d3e2f2024-12-18 11:20:17 -0500271auto
272 Manager::createFromEvent(sdbusplus::exception::generated_event_base&& event)
273 -> sdbusplus::message::object_path
Patrick Williams9ca4d132024-10-31 17:02:47 -0400274{
275 auto [msg, level, data] = lg2::details::extractEvent(std::move(event));
276 return this->createEntry(msg, level, std::move(data));
277}
278
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500279bool Manager::isQuiesceOnErrorEnabled()
280{
Patrick Williamsa5171972021-04-16 20:10:01 -0500281 // When running under tests, the Logging.Settings service will not be
282 // present. Assume false.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700283 if (IS_UNIT_TEST)
284 {
285 return false;
286 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500287
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500288 std::variant<bool> property;
289
290 auto method = this->busLog.new_method_call(
291 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
292 "org.freedesktop.DBus.Properties", "Get");
293
294 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
295
296 try
297 {
298 auto reply = this->busLog.call(method);
299 reply.read(property);
300 }
Patrick Williams45e83522022-07-22 19:26:52 -0500301 catch (const sdbusplus::exception_t& e)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500302 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500303 lg2::error("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
304 e);
Matt Spinler7ba17c32023-06-12 09:20:15 -0500305 return false;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500306 }
307
308 return std::get<bool>(property);
309}
310
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500311bool Manager::isCalloutPresent(const Entry& entry)
312{
Patrick Williamsea6d9c42024-12-11 14:58:21 -0500313 for (const auto& c : std::views::keys(entry.additionalData()))
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500314 {
315 if (c.find("CALLOUT_") != std::string::npos)
316 {
317 return true;
318 }
319 }
320
321 return false;
322}
323
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500324void Manager::findAndRemoveResolvedBlocks()
325{
326 for (auto& entry : entries)
327 {
328 if (entry.second->resolved())
329 {
330 checkAndRemoveBlockingError(entry.first);
331 }
332 }
333}
334
Patrick Williams45e83522022-07-22 19:26:52 -0500335void Manager::onEntryResolve(sdbusplus::message_t& msg)
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500336{
337 using Interface = std::string;
338 using Property = std::string;
339 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500340 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500341
342 Interface interface;
343 Properties properties;
344
345 msg.read(interface, properties);
346
347 for (const auto& p : properties)
348 {
349 if (p.first == "Resolved")
350 {
351 findAndRemoveResolvedBlocks();
352 return;
353 }
354 }
355}
356
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500357void Manager::checkAndQuiesceHost()
358{
Willy Tu6ddbf692023-09-05 10:54:16 -0700359 using Host = sdbusplus::server::xyz::openbmc_project::state::Host;
Patrick Williamsa144a272021-07-16 17:03:55 -0500360
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500361 // First check host state
Patrick Williamsa144a272021-07-16 17:03:55 -0500362 std::variant<Host::HostState> property;
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500363
364 auto method = this->busLog.new_method_call(
365 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
366 "org.freedesktop.DBus.Properties", "Get");
367
368 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
369
370 try
371 {
372 auto reply = this->busLog.call(method);
373 reply.read(property);
374 }
Patrick Williams45e83522022-07-22 19:26:52 -0500375 catch (const sdbusplus::exception_t& e)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500376 {
377 // Quiescing the host is a "best effort" type function. If unable to
378 // read the host state or it comes back empty, just return.
379 // The boot block object will still be created and the associations to
380 // find the log will be present. Don't want a dependency with
381 // phosphor-state-manager service
Patrick Williams5f285c52021-07-27 21:25:38 -0500382 lg2::info("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
383 e);
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500384 return;
385 }
386
Patrick Williamsa144a272021-07-16 17:03:55 -0500387 auto hostState = std::get<Host::HostState>(property);
388 if (hostState != Host::HostState::Running)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500389 {
390 return;
391 }
392
393 auto quiesce = this->busLog.new_method_call(
394 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
395 "org.freedesktop.systemd1.Manager", "StartUnit");
396
Andrew Geissler86a60e92022-04-15 14:36:02 -0500397 quiesce.append("obmc-host-graceful-quiesce@0.target");
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500398 quiesce.append("replace");
399
400 this->busLog.call_noreply(quiesce);
401}
402
Andrew Geissler32874542020-07-09 09:17:03 -0500403void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500404{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500405 // Verify we don't already have this entry blocking
Matt Spinler4a375952022-07-01 11:15:33 -0500406 auto it = find_if(this->blockingErrors.begin(), this->blockingErrors.end(),
407 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400408 return obj->entryId == entryId;
409 });
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500410 if (it != this->blockingErrors.end())
411 {
412 // Already recorded so just return
Patrick Williams5f285c52021-07-27 21:25:38 -0500413 lg2::debug(
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500414 "QuiesceOnError set and callout present but entry already logged");
415 return;
416 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500417
Patrick Williams5f285c52021-07-27 21:25:38 -0500418 lg2::info("QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500419
Patrick Williams075c7922024-08-16 15:19:49 -0400420 auto blockPath =
421 std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId);
Andrew Geissler32874542020-07-09 09:17:03 -0500422 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500423 this->blockingErrors.push_back(std::move(blockObj));
424
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500425 // Register call back if log is resolved
426 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500427 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Patrick Williams45e83522022-07-22 19:26:52 -0500428 auto callback = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500429 this->busLog,
430 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
431 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
432 std::placeholders::_1));
433
434 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500435 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500436
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500437 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500438}
439
Matt Spinlerc64b7122020-03-26 10:55:01 -0500440void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500441{
442 // Make the association <endpointpath>/<endpointtype> paths
443 std::vector<std::string> assocs;
444 for (const auto& [forwardType, reverseType, endpoint] :
445 entry.associations())
446 {
447 std::string e{endpoint};
448 e += '/' + reverseType;
449 assocs.push_back(e);
450 }
451
452 for (auto& create : Extensions::getCreateFunctions())
453 {
454 try
455 {
456 create(entry.message(), entry.id(), entry.timestamp(),
Patrick Williamse5940632024-11-22 20:47:58 -0500457 entry.severity(), entry.additionalData2(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500458 }
Patrick Williams66491c62021-10-06 12:23:37 -0500459 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500460 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500461 lg2::error(
462 "An extension's create function threw an exception: {ERROR}",
463 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500464 }
465 }
466}
467
Patrick Williamsf40323d2021-04-16 15:35:17 -0500468void Manager::processMetadata(const std::string& /*errorName*/,
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600469 const std::vector<std::string>& additionalData,
470 AssociationList& objects) const
471{
472 // additionalData is a list of "metadata=value"
473 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700474 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600475 {
Patrick Venture34438962018-10-30 13:17:37 -0700476 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700477 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600478 {
Patrick Venture34438962018-10-30 13:17:37 -0700479 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600480 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700481 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600482 {
483 (iter->second)(metadata, additionalData, objects);
484 }
485 }
486 }
487}
488
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500489void Manager::checkAndRemoveBlockingError(uint32_t entryId)
490{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500491 // First look for blocking object and remove
Matt Spinler4a375952022-07-01 11:15:33 -0500492 auto it = find_if(blockingErrors.begin(), blockingErrors.end(),
493 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400494 return obj->entryId == entryId;
495 });
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500496 if (it != blockingErrors.end())
497 {
498 blockingErrors.erase(it);
499 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500500
501 // Now remove the callback looking for the error to be resolved
502 auto resolveFind = propChangedEntryCallback.find(entryId);
503 if (resolveFind != propChangedEntryCallback.end())
504 {
505 propChangedEntryCallback.erase(resolveFind);
506 }
507
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500508 return;
509}
510
harsh-agarwal1d763db32024-09-03 09:18:50 -0500511size_t Manager::eraseAll()
512{
513 std::vector<uint32_t> logIDWithHwIsolation;
514 for (auto& func : Extensions::getLogIDWithHwIsolationFunctions())
515 {
516 try
517 {
518 func(logIDWithHwIsolation);
519 }
520 catch (const std::exception& e)
521 {
522 lg2::error("An extension's LogIDWithHwIsolation function threw an "
523 "exception: {ERROR}",
524 "ERROR", e);
525 }
526 }
527 size_t entriesSize = entries.size();
528 auto iter = entries.begin();
529 if (logIDWithHwIsolation.empty())
530 {
531 while (iter != entries.end())
532 {
533 auto e = iter->first;
534 ++iter;
535 erase(e);
536 }
537 entryId = 0;
538 }
539 else
540 {
541 while (iter != entries.end())
542 {
543 auto e = iter->first;
544 ++iter;
545 try
546 {
547 if (!std::ranges::contains(logIDWithHwIsolation, e))
548 {
549 erase(e);
550 }
551 else
552 {
553 entriesSize--;
554 }
555 }
556 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
557 Unavailable& e)
558 {
559 entriesSize--;
560 }
561 }
562 if (!entries.empty())
563 {
564 entryId = std::ranges::max_element(entries, [](const auto& a,
565 const auto& b) {
566 return a.first < b.first;
567 })->first;
568 }
569 else
570 {
571 entryId = 0;
572 }
573 }
574 return entriesSize;
575}
576
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500577void Manager::erase(uint32_t entryId)
578{
Patrick Venture34438962018-10-30 13:17:37 -0700579 auto entryFound = entries.find(entryId);
580 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500581 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500582 for (auto& func : Extensions::getDeleteProhibitedFunctions())
583 {
584 try
585 {
586 bool prohibited = false;
587 func(entryId, prohibited);
588 if (prohibited)
589 {
harsh-agarwal1d763db32024-09-03 09:18:50 -0500590 throw sdbusplus::xyz::openbmc_project::Common::Error::
591 Unavailable();
Matt Spinler99c2b402019-05-23 14:29:16 -0500592 }
593 }
harsh-agarwal1d763db32024-09-03 09:18:50 -0500594 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
595 Unavailable& e)
596 {
597 throw;
598 }
Patrick Williams66491c62021-10-06 12:23:37 -0500599 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500600 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500601 lg2::error("An extension's deleteProhibited function threw an "
602 "exception: {ERROR}",
603 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500604 }
605 }
606
Deepak Kodihalli33887992017-06-13 07:06:49 -0500607 // Delete the persistent representation of this error.
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400608 fs::path errorPath(paths::error());
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600609 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500610 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600611
Patrick Venturef18bf832018-10-26 18:14:00 -0700612 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600613 auto it = std::find(ids.begin(), ids.end(), id);
614 if (it != ids.end())
615 {
616 ids.erase(it);
617 }
618 };
Patrick Venture34438962018-10-30 13:17:37 -0700619 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500620 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600621 removeId(infoErrors, entryId);
622 }
623 else
624 {
625 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500626 }
Patrick Venture34438962018-10-30 13:17:37 -0700627 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500628
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500629 checkAndRemoveBlockingError(entryId);
630
Matt Spinler99c2b402019-05-23 14:29:16 -0500631 for (auto& remove : Extensions::getDeleteFunctions())
632 {
633 try
634 {
635 remove(entryId);
636 }
Patrick Williams66491c62021-10-06 12:23:37 -0500637 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500638 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500639 lg2::error("An extension's delete function threw an exception: "
640 "{ERROR}",
641 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500642 }
643 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500644 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600645 else
646 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500647 lg2::error("Invalid entry ID ({ID}) to delete", "ID", entryId);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600648 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500649}
650
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500651void Manager::restore()
652{
Patrick Venturef18bf832018-10-26 18:14:00 -0700653 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600654 return id == restoredId;
655 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500656
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400657 fs::path dir(paths::error());
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500658 if (!fs::exists(dir) || fs::is_empty(dir))
659 {
660 return;
661 }
662
Patrick Venturef18bf832018-10-26 18:14:00 -0700663 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500664 {
665 auto id = file.path().filename().c_str();
666 auto idNum = std::stol(id);
667 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700668 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500669 if (deserialize(file.path(), *e))
670 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700671 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600672 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500673 {
Matt Spinleref952af2021-08-19 10:23:05 -0500674 e->path(file.path(), true);
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600675 if (e->severity() >= Entry::sevLowerLimit)
676 {
677 infoErrors.push_back(idNum);
678 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600679 else
680 {
681 realErrors.push_back(idNum);
682 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600683
684 entries.insert(std::make_pair(idNum, std::move(e)));
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500685 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600686 else
687 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500688 lg2::error(
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600689 "Failed in sanity check while restoring error entry. "
Patrick Williams5f285c52021-07-27 21:25:38 -0500690 "Ignoring error entry {ID_NUM}/{ENTRY_ID}.",
691 "ID_NUM", idNum, "ENTRY_ID", e->id());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600692 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500693 }
694 }
695
Lei YU01bf5c42022-05-23 15:33:23 +0800696 if (!entries.empty())
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530697 {
Lei YU01bf5c42022-05-23 15:33:23 +0800698 entryId = entries.rbegin()->first;
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530699 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500700}
701
Matt Spinler1275bd12018-05-01 15:13:53 -0500702std::string Manager::readFWVersion()
703{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500704 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500705
Matt Spinlerf61f2922020-06-23 11:32:49 -0500706 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500707 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500708 lg2::error("Unable to read BMC firmware version");
Matt Spinler1275bd12018-05-01 15:13:53 -0500709 }
710
Matt Spinlerf61f2922020-06-23 11:32:49 -0500711 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500712}
713
Patrick Williams597f24a2024-09-27 14:47:42 -0400714auto Manager::create(const std::string& message, Entry::Level severity,
Paul Fertser221b79b2024-03-04 15:40:23 +0000715 const std::map<std::string, std::string>& additionalData,
Patrick Williams597f24a2024-09-27 14:47:42 -0400716 const FFDCEntries& ffdc) -> sdbusplus::message::object_path
Matt Spinlerc64b7122020-03-26 10:55:01 -0500717{
Patrick Williamsea21d992024-11-22 17:06:35 -0500718 return createEntry(message, severity, additionalData, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500719}
720
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500721} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600722} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700723} // namespace phosphor