blob: 7d54e12a0d14e0c22c59adb2d8c1e657362e5cc4 [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 Venturef18bf832018-10-26 18:14:00 -070030#include <set>
31#include <string>
Patrick Williamsb01a5b42021-08-28 15:11:45 -050032#include <string_view>
Patrick Venturef18bf832018-10-26 18:14:00 -070033#include <vector>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060034
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050035using namespace std::chrono;
Patrick Williams5f285c52021-07-27 21:25:38 -050036extern const std::map<
37 phosphor::logging::metadata::Metadata,
38 std::function<phosphor::logging::metadata::associations::Type>>
Patrick Venturef18bf832018-10-26 18:14:00 -070039 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050040
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060041namespace phosphor
42{
43namespace logging
44{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050045namespace internal
46{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050047inline auto getLevel(const std::string& errMsg)
48{
49 auto reqLevel = Entry::Level::Error; // Default to Error
50
51 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050052 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050053 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050054 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050055 }
56
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050057 return reqLevel;
58}
59
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050060int Manager::getRealErrSize()
61{
62 return realErrors.size();
63}
64
65int Manager::getInfoErrSize()
66{
67 return infoErrors.size();
68}
69
Lei YUb50c7052021-01-21 16:02:26 +080070uint32_t Manager::commit(uint64_t transactionId, std::string errMsg)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050071{
72 auto level = getLevel(errMsg);
73 _commit(transactionId, std::move(errMsg), level);
Lei YUb50c7052021-01-21 16:02:26 +080074 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050075}
76
Lei YUb50c7052021-01-21 16:02:26 +080077uint32_t Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
78 uint32_t errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050079{
80 _commit(transactionId, std::move(errMsg),
81 static_cast<Entry::Level>(errLvl));
Lei YUb50c7052021-01-21 16:02:26 +080082 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050083}
84
Patrick Williamsa5171972021-04-16 20:10:01 -050085void Manager::_commit(uint64_t transactionId [[maybe_unused]],
86 std::string&& errMsg, Entry::Level errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050087{
Patrick Williams64a9eaa2024-11-22 19:58:21 -050088 std::map<std::string, std::string> additionalData{};
Patrick Williamsa5171972021-04-16 20:10:01 -050089
90 // When running as a test-case, the system may have a LOT of journal
91 // data and we may not have permissions to do some of the journal sync
92 // operations. Just skip over them.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070093 if (!IS_UNIT_TEST)
Adriana Kobylakd311bc82016-10-16 09:54:40 -050094 {
Patrick Williamsb01a5b42021-08-28 15:11:45 -050095 static constexpr auto transactionIdVar =
96 std::string_view{"TRANSACTION_ID"};
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 transactionIdVarSize = transactionIdVar.size();
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070099 // Length of 'TRANSACTION_ID=' string.
Patrick Williamsb01a5b42021-08-28 15:11:45 -0500100 static constexpr auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500101
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700102 // Flush all the pending log messages into the journal
Matt Spinler271d1432023-01-18 13:58:05 -0600103 util::journalSync();
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600104
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700105 sd_journal* j = nullptr;
106 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600107 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600108 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500109 lg2::error("Failed to open journal: {ERROR}", "ERROR",
110 strerror(-rc));
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700111 return;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600112 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600113
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700114 std::string transactionIdStr = std::to_string(transactionId);
115 std::set<std::string> metalist;
116 auto metamap = g_errMetaMap.find(errMsg);
117 if (metamap != g_errMetaMap.end())
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600118 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700119 metalist.insert(metamap->second.begin(), metamap->second.end());
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600120 }
121
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700122 // Add _PID field information in AdditionalData.
123 metalist.insert("_PID");
124
125 // Read the journal from the end to get the most recent entry first.
126 // The result from the sd_journal_get_data() is of the form
127 // VARIABLE=value.
128 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600129 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700130 const char* data = nullptr;
131 size_t length = 0;
132
133 // Look for the transaction id metadata variable
Patrick Williamsb01a5b42021-08-28 15:11:45 -0500134 rc = sd_journal_get_data(j, transactionIdVar.data(),
135 (const void**)&data, &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600136 if (rc < 0)
137 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700138 // This journal entry does not have the TRANSACTION_ID
139 // metadata variable.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600140 continue;
141 }
142
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700143 // journald does not guarantee that sd_journal_get_data() returns
144 // NULL terminated strings, so need to specify the size to use to
145 // compare, use the returned length instead of anything that relies
146 // on NULL terminators like strlen(). The data variable is in the
147 // form of 'TRANSACTION_ID=1234'. Remove the TRANSACTION_ID
148 // characters plus the (=) sign to do the comparison. 'data +
149 // transactionIdVarOffset' will be in the form of '1234'. 'length -
150 // transactionIdVarOffset' will be the length of '1234'.
151 if ((length <= (transactionIdVarOffset)) ||
152 (transactionIdStr.compare(
153 0, transactionIdStr.size(), data + transactionIdVarOffset,
154 length - transactionIdVarOffset) != 0))
155 {
156 // The value of the TRANSACTION_ID metadata is not the requested
157 // transaction id number.
158 continue;
159 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600160
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700161 // Search for all metadata variables in the current journal entry.
162 for (auto i = metalist.cbegin(); i != metalist.cend();)
163 {
164 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
165 &length);
166 if (rc < 0)
167 {
168 // Metadata variable not found, check next metadata
169 // variable.
170 i++;
171 continue;
172 }
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500173
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700174 // Metadata variable found, save it and remove it from the set.
Patrick Williams64a9eaa2024-11-22 19:58:21 -0500175 std::string metadata(data, length);
176 if (auto pos = metadata.find('='); pos != std::string::npos)
177 {
178 auto key = metadata.substr(0, pos);
179 auto value = metadata.substr(pos + 1);
180 additionalData.emplace(std::move(key), std::move(value));
181 }
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700182 i = metalist.erase(i);
183 }
184 if (metalist.empty())
185 {
186 // All metadata variables found, break out of journal loop.
187 break;
188 }
189 }
190 if (!metalist.empty())
191 {
192 // Not all the metadata variables were found in the journal.
193 for (auto& metaVarStr : metalist)
194 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500195 lg2::info("Failed to find metadata: {META_FIELD}", "META_FIELD",
196 metaVarStr);
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700197 }
198 }
199
200 sd_journal_close(j);
201 }
Patrick Williams64a9eaa2024-11-22 19:58:21 -0500202 createEntry(errMsg, errLvl, additionalData);
Matt Spinlerb60e7552019-07-24 15:28:08 -0500203}
204
Patrick Williams597f24a2024-09-27 14:47:42 -0400205auto Manager::createEntry(
206 std::string errMsg, Entry::Level errLvl,
Patrick Williamsea21d992024-11-22 17:06:35 -0500207 std::map<std::string, std::string> additionalData,
Patrick Williams597f24a2024-09-27 14:47:42 -0400208 const FFDCEntries& ffdc) -> sdbusplus::message::object_path
Matt Spinlerb60e7552019-07-24 15:28:08 -0500209{
210 if (!Extensions::disableDefaultLogCaps())
211 {
212 if (errLvl < Entry::sevLowerLimit)
213 {
214 if (realErrors.size() >= ERROR_CAP)
215 {
216 erase(realErrors.front());
217 }
218 }
219 else
220 {
221 if (infoErrors.size() >= ERROR_INFO_CAP)
222 {
223 erase(infoErrors.front());
224 }
225 }
226 }
227
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600228 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500229 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500230 {
231 infoErrors.push_back(entryId);
232 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600233 else
234 {
235 realErrors.push_back(entryId);
236 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600237 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700238 std::chrono::system_clock::now().time_since_epoch())
239 .count();
240 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600241
Patrick Venturef18bf832018-10-26 18:14:00 -0700242 AssociationList objects{};
Patrick Williamsea21d992024-11-22 17:06:35 -0500243 auto additionalDataVec = util::additional_data::combine(additionalData);
244 processMetadata(errMsg, additionalDataVec, objects);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600245
Matt Spinlerfb978da2022-01-21 08:42:24 -0600246 auto e = std::make_unique<Entry>(
247 busLog, objPath, entryId,
248 ms, // Milliseconds since 1970
249 errLvl, std::move(errMsg), std::move(additionalData),
250 std::move(objects), fwVersion, getEntrySerializePath(entryId), *this);
251
252 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500253
Matt Spinler601b8112022-01-26 13:18:04 -0600254 if (isQuiesceOnErrorEnabled() && (errLvl < Entry::sevLowerLimit) &&
255 isCalloutPresent(*e))
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500256 {
Andrew Geissler32874542020-07-09 09:17:03 -0500257 quiesceOnError(entryId);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500258 }
259
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600260 // Add entry before calling the extensions so that they have access to it
261 entries.insert(std::make_pair(entryId, std::move(e)));
262
263 doExtensionLogCreate(*entries.find(entryId)->second, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500264
265 // Note: No need to close the file descriptors in the FFDC.
Patrick Williams597f24a2024-09-27 14:47:42 -0400266
267 return objPath;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500268}
269
Patrick Williams9ca4d132024-10-31 17:02:47 -0400270auto Manager::createFromEvent(sdbusplus::exception::generated_event_base&&
271 event) -> sdbusplus::message::object_path
272{
273 auto [msg, level, data] = lg2::details::extractEvent(std::move(event));
274 return this->createEntry(msg, level, std::move(data));
275}
276
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500277bool Manager::isQuiesceOnErrorEnabled()
278{
Patrick Williamsa5171972021-04-16 20:10:01 -0500279 // When running under tests, the Logging.Settings service will not be
280 // present. Assume false.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700281 if (IS_UNIT_TEST)
282 {
283 return false;
284 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500285
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500286 std::variant<bool> property;
287
288 auto method = this->busLog.new_method_call(
289 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
290 "org.freedesktop.DBus.Properties", "Get");
291
292 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
293
294 try
295 {
296 auto reply = this->busLog.call(method);
297 reply.read(property);
298 }
Patrick Williams45e83522022-07-22 19:26:52 -0500299 catch (const sdbusplus::exception_t& e)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500300 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500301 lg2::error("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
302 e);
Matt Spinler7ba17c32023-06-12 09:20:15 -0500303 return false;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500304 }
305
306 return std::get<bool>(property);
307}
308
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500309bool Manager::isCalloutPresent(const Entry& entry)
310{
311 for (const auto& c : entry.additionalData())
312 {
313 if (c.find("CALLOUT_") != std::string::npos)
314 {
315 return true;
316 }
317 }
318
319 return false;
320}
321
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500322void Manager::findAndRemoveResolvedBlocks()
323{
324 for (auto& entry : entries)
325 {
326 if (entry.second->resolved())
327 {
328 checkAndRemoveBlockingError(entry.first);
329 }
330 }
331}
332
Patrick Williams45e83522022-07-22 19:26:52 -0500333void Manager::onEntryResolve(sdbusplus::message_t& msg)
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500334{
335 using Interface = std::string;
336 using Property = std::string;
337 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500338 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500339
340 Interface interface;
341 Properties properties;
342
343 msg.read(interface, properties);
344
345 for (const auto& p : properties)
346 {
347 if (p.first == "Resolved")
348 {
349 findAndRemoveResolvedBlocks();
350 return;
351 }
352 }
353}
354
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500355void Manager::checkAndQuiesceHost()
356{
Willy Tu6ddbf692023-09-05 10:54:16 -0700357 using Host = sdbusplus::server::xyz::openbmc_project::state::Host;
Patrick Williamsa144a272021-07-16 17:03:55 -0500358
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500359 // First check host state
Patrick Williamsa144a272021-07-16 17:03:55 -0500360 std::variant<Host::HostState> property;
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500361
362 auto method = this->busLog.new_method_call(
363 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
364 "org.freedesktop.DBus.Properties", "Get");
365
366 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
367
368 try
369 {
370 auto reply = this->busLog.call(method);
371 reply.read(property);
372 }
Patrick Williams45e83522022-07-22 19:26:52 -0500373 catch (const sdbusplus::exception_t& e)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500374 {
375 // Quiescing the host is a "best effort" type function. If unable to
376 // read the host state or it comes back empty, just return.
377 // The boot block object will still be created and the associations to
378 // find the log will be present. Don't want a dependency with
379 // phosphor-state-manager service
Patrick Williams5f285c52021-07-27 21:25:38 -0500380 lg2::info("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
381 e);
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500382 return;
383 }
384
Patrick Williamsa144a272021-07-16 17:03:55 -0500385 auto hostState = std::get<Host::HostState>(property);
386 if (hostState != Host::HostState::Running)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500387 {
388 return;
389 }
390
391 auto quiesce = this->busLog.new_method_call(
392 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
393 "org.freedesktop.systemd1.Manager", "StartUnit");
394
Andrew Geissler86a60e92022-04-15 14:36:02 -0500395 quiesce.append("obmc-host-graceful-quiesce@0.target");
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500396 quiesce.append("replace");
397
398 this->busLog.call_noreply(quiesce);
399}
400
Andrew Geissler32874542020-07-09 09:17:03 -0500401void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500402{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500403 // Verify we don't already have this entry blocking
Matt Spinler4a375952022-07-01 11:15:33 -0500404 auto it = find_if(this->blockingErrors.begin(), this->blockingErrors.end(),
405 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400406 return obj->entryId == entryId;
407 });
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500408 if (it != this->blockingErrors.end())
409 {
410 // Already recorded so just return
Patrick Williams5f285c52021-07-27 21:25:38 -0500411 lg2::debug(
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500412 "QuiesceOnError set and callout present but entry already logged");
413 return;
414 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500415
Patrick Williams5f285c52021-07-27 21:25:38 -0500416 lg2::info("QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500417
Patrick Williams075c7922024-08-16 15:19:49 -0400418 auto blockPath =
419 std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId);
Andrew Geissler32874542020-07-09 09:17:03 -0500420 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500421 this->blockingErrors.push_back(std::move(blockObj));
422
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500423 // Register call back if log is resolved
424 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500425 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Patrick Williams45e83522022-07-22 19:26:52 -0500426 auto callback = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500427 this->busLog,
428 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
429 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
430 std::placeholders::_1));
431
432 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500433 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500434
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500435 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500436}
437
Matt Spinlerc64b7122020-03-26 10:55:01 -0500438void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500439{
440 // Make the association <endpointpath>/<endpointtype> paths
441 std::vector<std::string> assocs;
442 for (const auto& [forwardType, reverseType, endpoint] :
443 entry.associations())
444 {
445 std::string e{endpoint};
446 e += '/' + reverseType;
447 assocs.push_back(e);
448 }
449
450 for (auto& create : Extensions::getCreateFunctions())
451 {
452 try
453 {
454 create(entry.message(), entry.id(), entry.timestamp(),
Patrick Williamse5940632024-11-22 20:47:58 -0500455 entry.severity(), entry.additionalData2(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500456 }
Patrick Williams66491c62021-10-06 12:23:37 -0500457 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500458 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500459 lg2::error(
460 "An extension's create function threw an exception: {ERROR}",
461 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500462 }
463 }
464}
465
Patrick Williamsf40323d2021-04-16 15:35:17 -0500466void Manager::processMetadata(const std::string& /*errorName*/,
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600467 const std::vector<std::string>& additionalData,
468 AssociationList& objects) const
469{
470 // additionalData is a list of "metadata=value"
471 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700472 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600473 {
Patrick Venture34438962018-10-30 13:17:37 -0700474 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700475 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600476 {
Patrick Venture34438962018-10-30 13:17:37 -0700477 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600478 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700479 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600480 {
481 (iter->second)(metadata, additionalData, objects);
482 }
483 }
484 }
485}
486
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500487void Manager::checkAndRemoveBlockingError(uint32_t entryId)
488{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500489 // First look for blocking object and remove
Matt Spinler4a375952022-07-01 11:15:33 -0500490 auto it = find_if(blockingErrors.begin(), blockingErrors.end(),
491 [&](const std::unique_ptr<Block>& obj) {
Patrick Williams075c7922024-08-16 15:19:49 -0400492 return obj->entryId == entryId;
493 });
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500494 if (it != blockingErrors.end())
495 {
496 blockingErrors.erase(it);
497 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500498
499 // Now remove the callback looking for the error to be resolved
500 auto resolveFind = propChangedEntryCallback.find(entryId);
501 if (resolveFind != propChangedEntryCallback.end())
502 {
503 propChangedEntryCallback.erase(resolveFind);
504 }
505
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500506 return;
507}
508
harsh-agarwal1d763db32024-09-03 09:18:50 -0500509size_t Manager::eraseAll()
510{
511 std::vector<uint32_t> logIDWithHwIsolation;
512 for (auto& func : Extensions::getLogIDWithHwIsolationFunctions())
513 {
514 try
515 {
516 func(logIDWithHwIsolation);
517 }
518 catch (const std::exception& e)
519 {
520 lg2::error("An extension's LogIDWithHwIsolation function threw an "
521 "exception: {ERROR}",
522 "ERROR", e);
523 }
524 }
525 size_t entriesSize = entries.size();
526 auto iter = entries.begin();
527 if (logIDWithHwIsolation.empty())
528 {
529 while (iter != entries.end())
530 {
531 auto e = iter->first;
532 ++iter;
533 erase(e);
534 }
535 entryId = 0;
536 }
537 else
538 {
539 while (iter != entries.end())
540 {
541 auto e = iter->first;
542 ++iter;
543 try
544 {
545 if (!std::ranges::contains(logIDWithHwIsolation, e))
546 {
547 erase(e);
548 }
549 else
550 {
551 entriesSize--;
552 }
553 }
554 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
555 Unavailable& e)
556 {
557 entriesSize--;
558 }
559 }
560 if (!entries.empty())
561 {
562 entryId = std::ranges::max_element(entries, [](const auto& a,
563 const auto& b) {
564 return a.first < b.first;
565 })->first;
566 }
567 else
568 {
569 entryId = 0;
570 }
571 }
572 return entriesSize;
573}
574
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500575void Manager::erase(uint32_t entryId)
576{
Patrick Venture34438962018-10-30 13:17:37 -0700577 auto entryFound = entries.find(entryId);
578 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500579 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500580 for (auto& func : Extensions::getDeleteProhibitedFunctions())
581 {
582 try
583 {
584 bool prohibited = false;
585 func(entryId, prohibited);
586 if (prohibited)
587 {
harsh-agarwal1d763db32024-09-03 09:18:50 -0500588 throw sdbusplus::xyz::openbmc_project::Common::Error::
589 Unavailable();
Matt Spinler99c2b402019-05-23 14:29:16 -0500590 }
591 }
harsh-agarwal1d763db32024-09-03 09:18:50 -0500592 catch (const sdbusplus::xyz::openbmc_project::Common::Error::
593 Unavailable& e)
594 {
595 throw;
596 }
Patrick Williams66491c62021-10-06 12:23:37 -0500597 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500598 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500599 lg2::error("An extension's deleteProhibited function threw an "
600 "exception: {ERROR}",
601 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500602 }
603 }
604
Deepak Kodihalli33887992017-06-13 07:06:49 -0500605 // Delete the persistent representation of this error.
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400606 fs::path errorPath(paths::error());
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600607 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500608 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600609
Patrick Venturef18bf832018-10-26 18:14:00 -0700610 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600611 auto it = std::find(ids.begin(), ids.end(), id);
612 if (it != ids.end())
613 {
614 ids.erase(it);
615 }
616 };
Patrick Venture34438962018-10-30 13:17:37 -0700617 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500618 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600619 removeId(infoErrors, entryId);
620 }
621 else
622 {
623 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500624 }
Patrick Venture34438962018-10-30 13:17:37 -0700625 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500626
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500627 checkAndRemoveBlockingError(entryId);
628
Matt Spinler99c2b402019-05-23 14:29:16 -0500629 for (auto& remove : Extensions::getDeleteFunctions())
630 {
631 try
632 {
633 remove(entryId);
634 }
Patrick Williams66491c62021-10-06 12:23:37 -0500635 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500636 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500637 lg2::error("An extension's delete function threw an exception: "
638 "{ERROR}",
639 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500640 }
641 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500642 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600643 else
644 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500645 lg2::error("Invalid entry ID ({ID}) to delete", "ID", entryId);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600646 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500647}
648
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500649void Manager::restore()
650{
Patrick Venturef18bf832018-10-26 18:14:00 -0700651 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600652 return id == restoredId;
653 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500654
Patrick Williamsfa2d9622024-09-30 16:25:43 -0400655 fs::path dir(paths::error());
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500656 if (!fs::exists(dir) || fs::is_empty(dir))
657 {
658 return;
659 }
660
Patrick Venturef18bf832018-10-26 18:14:00 -0700661 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500662 {
663 auto id = file.path().filename().c_str();
664 auto idNum = std::stol(id);
665 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700666 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500667 if (deserialize(file.path(), *e))
668 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700669 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600670 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500671 {
Matt Spinleref952af2021-08-19 10:23:05 -0500672 e->path(file.path(), true);
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600673 if (e->severity() >= Entry::sevLowerLimit)
674 {
675 infoErrors.push_back(idNum);
676 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600677 else
678 {
679 realErrors.push_back(idNum);
680 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600681
682 entries.insert(std::make_pair(idNum, std::move(e)));
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500683 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600684 else
685 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500686 lg2::error(
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600687 "Failed in sanity check while restoring error entry. "
Patrick Williams5f285c52021-07-27 21:25:38 -0500688 "Ignoring error entry {ID_NUM}/{ENTRY_ID}.",
689 "ID_NUM", idNum, "ENTRY_ID", e->id());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600690 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500691 }
692 }
693
Lei YU01bf5c42022-05-23 15:33:23 +0800694 if (!entries.empty())
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530695 {
Lei YU01bf5c42022-05-23 15:33:23 +0800696 entryId = entries.rbegin()->first;
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530697 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500698}
699
Matt Spinler1275bd12018-05-01 15:13:53 -0500700std::string Manager::readFWVersion()
701{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500702 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500703
Matt Spinlerf61f2922020-06-23 11:32:49 -0500704 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500705 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500706 lg2::error("Unable to read BMC firmware version");
Matt Spinler1275bd12018-05-01 15:13:53 -0500707 }
708
Matt Spinlerf61f2922020-06-23 11:32:49 -0500709 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500710}
711
Patrick Williams597f24a2024-09-27 14:47:42 -0400712auto Manager::create(const std::string& message, Entry::Level severity,
Paul Fertser221b79b2024-03-04 15:40:23 +0000713 const std::map<std::string, std::string>& additionalData,
Patrick Williams597f24a2024-09-27 14:47:42 -0400714 const FFDCEntries& ffdc) -> sdbusplus::message::object_path
Matt Spinlerc64b7122020-03-26 10:55:01 -0500715{
Patrick Williamsea21d992024-11-22 17:06:35 -0500716 return createEntry(message, severity, additionalData, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500717}
718
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500719} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600720} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700721} // namespace phosphor