blob: 83ad1687a8aa1599dbe91c31fdb01ace2810604e [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"
Matt Spinlerf61f2922020-06-23 11:32:49 -05009#include "util.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -070010
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050011#include <poll.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050012#include <sys/inotify.h>
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
Matt Spinler99c2b402019-05-23 14:29:16 -050017#include <cassert>
Patrick Venturef18bf832018-10-26 18:14:00 -070018#include <chrono>
19#include <cstdio>
Patrick Venture30047bf2018-11-01 18:52:15 -070020#include <cstring>
Patrick Venturef18bf832018-10-26 18:14:00 -070021#include <fstream>
Patrick Venture30047bf2018-11-01 18:52:15 -070022#include <functional>
Patrick Venturef18bf832018-10-26 18:14:00 -070023#include <future>
24#include <iostream>
Patrick Venture30047bf2018-11-01 18:52:15 -070025#include <map>
Saqib Khan2bb15192017-02-13 13:19:55 -060026#include <phosphor-logging/log.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -070027#include <sdbusplus/vtable.hpp>
28#include <set>
29#include <string>
30#include <vector>
Andrew Geisslerf6126a72020-05-08 10:41:09 -050031#include <xyz/openbmc_project/State/Host/server.hpp>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060032
33using namespace phosphor::logging;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050034using namespace std::chrono;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -050035using sdbusplus::exception::SdBusError;
Deepak Kodihallia87c1572017-02-28 07:40:34 -060036extern const std::map<metadata::Metadata,
Patrick Venturef18bf832018-10-26 18:14:00 -070037 std::function<metadata::associations::Type>>
38 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050039
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060040namespace phosphor
41{
42namespace logging
43{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050044namespace internal
45{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050046inline auto getLevel(const std::string& errMsg)
47{
48 auto reqLevel = Entry::Level::Error; // Default to Error
49
50 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050051 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050052 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050053 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050054 }
55
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050056 return reqLevel;
57}
58
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050059int Manager::getRealErrSize()
60{
61 return realErrors.size();
62}
63
64int Manager::getInfoErrSize()
65{
66 return infoErrors.size();
67}
68
Lei YUb50c7052021-01-21 16:02:26 +080069uint32_t Manager::commit(uint64_t transactionId, std::string errMsg)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050070{
71 auto level = getLevel(errMsg);
72 _commit(transactionId, std::move(errMsg), level);
Lei YUb50c7052021-01-21 16:02:26 +080073 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050074}
75
Lei YUb50c7052021-01-21 16:02:26 +080076uint32_t Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
77 uint32_t errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050078{
79 _commit(transactionId, std::move(errMsg),
80 static_cast<Entry::Level>(errLvl));
Lei YUb50c7052021-01-21 16:02:26 +080081 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050082}
83
Patrick Williamsa5171972021-04-16 20:10:01 -050084void Manager::_commit(uint64_t transactionId [[maybe_unused]],
85 std::string&& errMsg, Entry::Level errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050086{
Patrick Williamsa5171972021-04-16 20:10:01 -050087 std::vector<std::string> additionalData{};
88
89 // When running as a test-case, the system may have a LOT of journal
90 // data and we may not have permissions to do some of the journal sync
91 // operations. Just skip over them.
92#ifndef TESTCASE
93
Adriana Kobylak7298dc22017-01-24 12:21:50 -060094 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -060095 // Length of 'TRANSACTION_ID' string.
Patrick Venture30047bf2018-11-01 18:52:15 -070096 constexpr const auto transactionIdVarSize = std::strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -060097 // Length of 'TRANSACTION_ID=' string.
98 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050099
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500100 // Flush all the pending log messages into the journal
101 journalSync();
Adriana Kobylakcfd9a7d2017-06-07 11:57:31 -0500102
Patrick Venturef18bf832018-10-26 18:14:00 -0700103 sd_journal* j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600104 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500105 if (rc < 0)
106 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700107 logging::log<logging::level::ERR>(
108 "Failed to open journal",
109 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600110 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500111 }
112
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600113 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -0600114 std::set<std::string> metalist;
115 auto metamap = g_errMetaMap.find(errMsg);
116 if (metamap != g_errMetaMap.end())
117 {
118 metalist.insert(metamap->second.begin(), metamap->second.end());
119 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600120
Patrick Venturef18bf832018-10-26 18:14:00 -0700121 // Add _PID field information in AdditionalData.
Jayanth Othayothdb18ebe2017-09-04 00:48:02 -0500122 metalist.insert("_PID");
123
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600124 // Read the journal from the end to get the most recent entry first.
125 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
126 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600127 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700128 const char* data = nullptr;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600129 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600130
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600131 // Look for the transaction id metadata variable
Patrick Venturef18bf832018-10-26 18:14:00 -0700132 rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data,
133 &length);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600134 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600135 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600136 // This journal entry does not have the TRANSACTION_ID
137 // metadata variable.
138 continue;
139 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600140
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600141 // journald does not guarantee that sd_journal_get_data() returns NULL
142 // terminated strings, so need to specify the size to use to compare,
143 // use the returned length instead of anything that relies on NULL
144 // terminators like strlen().
145 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
146 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
147 // 'data + transactionIdVarOffset' will be in the form of '1234'.
148 // 'length - transactionIdVarOffset' will be the length of '1234'.
149 if ((length <= (transactionIdVarOffset)) ||
Patrick Venturef18bf832018-10-26 18:14:00 -0700150 (transactionIdStr.compare(0, transactionIdStr.size(),
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600151 data + transactionIdVarOffset,
152 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600153 {
154 // The value of the TRANSACTION_ID metadata is not the requested
155 // transaction id number.
156 continue;
157 }
158
159 // Search for all metadata variables in the current journal entry.
160 for (auto i = metalist.cbegin(); i != metalist.cend();)
161 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700162 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
163 &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600164 if (rc < 0)
165 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600166 // Metadata variable not found, check next metadata variable.
167 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600168 continue;
169 }
170
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600171 // Metadata variable found, save it and remove it from the set.
172 additionalData.emplace_back(data, length);
173 i = metalist.erase(i);
174 }
175 if (metalist.empty())
176 {
177 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600178 break;
179 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600180 }
181 if (!metalist.empty())
182 {
183 // Not all the metadata variables were found in the journal.
184 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600185 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700186 logging::log<logging::level::INFO>(
187 "Failed to find metadata",
188 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600189 }
190 }
191
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500192 sd_journal_close(j);
193
Patrick Williamsa5171972021-04-16 20:10:01 -0500194#endif
Matt Spinlerb60e7552019-07-24 15:28:08 -0500195 createEntry(errMsg, errLvl, additionalData);
196}
197
198void Manager::createEntry(std::string errMsg, Entry::Level errLvl,
Matt Spinlerc64b7122020-03-26 10:55:01 -0500199 std::vector<std::string> additionalData,
200 const FFDCEntries& ffdc)
Matt Spinlerb60e7552019-07-24 15:28:08 -0500201{
202 if (!Extensions::disableDefaultLogCaps())
203 {
204 if (errLvl < Entry::sevLowerLimit)
205 {
206 if (realErrors.size() >= ERROR_CAP)
207 {
208 erase(realErrors.front());
209 }
210 }
211 else
212 {
213 if (infoErrors.size() >= ERROR_INFO_CAP)
214 {
215 erase(infoErrors.front());
216 }
217 }
218 }
219
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600220 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500221 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500222 {
223 infoErrors.push_back(entryId);
224 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600225 else
226 {
227 realErrors.push_back(entryId);
228 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600229 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700230 std::chrono::system_clock::now().time_since_epoch())
231 .count();
232 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600233
Patrick Venturef18bf832018-10-26 18:14:00 -0700234 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600235 processMetadata(errMsg, additionalData, objects);
236
Patrick Venturef18bf832018-10-26 18:14:00 -0700237 auto e = std::make_unique<Entry>(busLog, objPath, entryId,
238 ms, // Milliseconds since 1970
239 errLvl, std::move(errMsg),
240 std::move(additionalData),
241 std::move(objects), fwVersion, *this);
Adriana Kobylak1ff95ef2020-12-03 13:52:19 -0600242 auto path = serialize(*e);
243 e->path(path);
Matt Spinler99c2b402019-05-23 14:29:16 -0500244
Andrew Geissler32874542020-07-09 09:17:03 -0500245 if (isQuiesceOnErrorEnabled() && isCalloutPresent(*e))
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500246 {
Andrew Geissler32874542020-07-09 09:17:03 -0500247 quiesceOnError(entryId);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500248 }
249
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600250 // Add entry before calling the extensions so that they have access to it
251 entries.insert(std::make_pair(entryId, std::move(e)));
252
253 doExtensionLogCreate(*entries.find(entryId)->second, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500254
255 // Note: No need to close the file descriptors in the FFDC.
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500256}
257
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500258bool Manager::isQuiesceOnErrorEnabled()
259{
Patrick Williamsa5171972021-04-16 20:10:01 -0500260 // When running under tests, the Logging.Settings service will not be
261 // present. Assume false.
262#ifdef TESTCASE
263 return false;
264#endif
265
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500266 std::variant<bool> property;
267
268 auto method = this->busLog.new_method_call(
269 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
270 "org.freedesktop.DBus.Properties", "Get");
271
272 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
273
274 try
275 {
276 auto reply = this->busLog.call(method);
277 reply.read(property);
278 }
279 catch (const SdBusError& e)
280 {
281 log<level::ERR>("Error reading QuiesceOnHwError property",
282 entry("ERROR=%s", e.what()));
283 throw;
284 }
285
286 return std::get<bool>(property);
287}
288
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500289bool Manager::isCalloutPresent(const Entry& entry)
290{
291 for (const auto& c : entry.additionalData())
292 {
293 if (c.find("CALLOUT_") != std::string::npos)
294 {
295 return true;
296 }
297 }
298
299 return false;
300}
301
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500302void Manager::findAndRemoveResolvedBlocks()
303{
304 for (auto& entry : entries)
305 {
306 if (entry.second->resolved())
307 {
308 checkAndRemoveBlockingError(entry.first);
309 }
310 }
311}
312
313void Manager::onEntryResolve(sdbusplus::message::message& msg)
314{
315 using Interface = std::string;
316 using Property = std::string;
317 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500318 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500319
320 Interface interface;
321 Properties properties;
322
323 msg.read(interface, properties);
324
325 for (const auto& p : properties)
326 {
327 if (p.first == "Resolved")
328 {
329 findAndRemoveResolvedBlocks();
330 return;
331 }
332 }
333}
334
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500335void Manager::checkAndQuiesceHost()
336{
337 // First check host state
338 std::variant<std::string> property;
339
340 auto method = this->busLog.new_method_call(
341 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
342 "org.freedesktop.DBus.Properties", "Get");
343
344 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
345
346 try
347 {
348 auto reply = this->busLog.call(method);
349 reply.read(property);
350 }
351 catch (const SdBusError& e)
352 {
353 // Quiescing the host is a "best effort" type function. If unable to
354 // read the host state or it comes back empty, just return.
355 // The boot block object will still be created and the associations to
356 // find the log will be present. Don't want a dependency with
357 // phosphor-state-manager service
358 log<level::INFO>("Error reading QuiesceOnHwError property",
359 entry("ERROR=%s", e.what()));
360 return;
361 }
362
363 std::string hostState = std::get<std::string>(property);
364
365 // If host state is empty, do nothing
366 if (hostState.empty())
367 {
368 return;
369 }
370
371 using Host = sdbusplus::xyz::openbmc_project::State::server::Host;
372 auto state = Host::convertHostStateFromString(hostState);
373 if (state != Host::HostState::Running)
374 {
375 return;
376 }
377
378 auto quiesce = this->busLog.new_method_call(
379 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
380 "org.freedesktop.systemd1.Manager", "StartUnit");
381
382 quiesce.append("obmc-host-quiesce@0.target");
383 quiesce.append("replace");
384
385 this->busLog.call_noreply(quiesce);
386}
387
Andrew Geissler32874542020-07-09 09:17:03 -0500388void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500389{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500390 // Verify we don't already have this entry blocking
391 auto it = find_if(
392 this->blockingErrors.begin(), this->blockingErrors.end(),
393 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
394 if (it != this->blockingErrors.end())
395 {
396 // Already recorded so just return
397 logging::log<logging::level::DEBUG>(
398 "QuiesceOnError set and callout present but entry already logged");
399 return;
400 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500401
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500402 logging::log<logging::level::INFO>(
403 "QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500404
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500405 auto blockPath =
Andrew Geissler32874542020-07-09 09:17:03 -0500406 std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId);
407 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500408 this->blockingErrors.push_back(std::move(blockObj));
409
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500410 // Register call back if log is resolved
411 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500412 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500413 auto callback = std::make_unique<sdbusplus::bus::match::match>(
414 this->busLog,
415 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
416 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
417 std::placeholders::_1));
418
419 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500420 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500421
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500422 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500423}
424
Matt Spinlerc64b7122020-03-26 10:55:01 -0500425void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500426{
427 // Make the association <endpointpath>/<endpointtype> paths
428 std::vector<std::string> assocs;
429 for (const auto& [forwardType, reverseType, endpoint] :
430 entry.associations())
431 {
432 std::string e{endpoint};
433 e += '/' + reverseType;
434 assocs.push_back(e);
435 }
436
437 for (auto& create : Extensions::getCreateFunctions())
438 {
439 try
440 {
441 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500442 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500443 }
444 catch (std::exception& e)
445 {
446 log<level::ERR>("An extension's create function threw an exception",
447 phosphor::logging::entry("ERROR=%s", e.what()));
448 }
449 }
450}
451
Patrick Williamsf40323d2021-04-16 15:35:17 -0500452void Manager::processMetadata(const std::string& /*errorName*/,
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600453 const std::vector<std::string>& additionalData,
454 AssociationList& objects) const
455{
456 // additionalData is a list of "metadata=value"
457 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700458 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600459 {
Patrick Venture34438962018-10-30 13:17:37 -0700460 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700461 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600462 {
Patrick Venture34438962018-10-30 13:17:37 -0700463 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600464 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700465 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600466 {
467 (iter->second)(metadata, additionalData, objects);
468 }
469 }
470 }
471}
472
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500473void Manager::checkAndRemoveBlockingError(uint32_t entryId)
474{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500475 // First look for blocking object and remove
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500476 auto it = find_if(
477 blockingErrors.begin(), blockingErrors.end(),
478 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
479 if (it != blockingErrors.end())
480 {
481 blockingErrors.erase(it);
482 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500483
484 // Now remove the callback looking for the error to be resolved
485 auto resolveFind = propChangedEntryCallback.find(entryId);
486 if (resolveFind != propChangedEntryCallback.end())
487 {
488 propChangedEntryCallback.erase(resolveFind);
489 }
490
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500491 return;
492}
493
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500494void Manager::erase(uint32_t entryId)
495{
Patrick Venture34438962018-10-30 13:17:37 -0700496 auto entryFound = entries.find(entryId);
497 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500498 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500499 for (auto& func : Extensions::getDeleteProhibitedFunctions())
500 {
501 try
502 {
503 bool prohibited = false;
504 func(entryId, prohibited);
505 if (prohibited)
506 {
507 // Future work remains to throw an error here.
508 return;
509 }
510 }
511 catch (std::exception& e)
512 {
513 log<level::ERR>(
514 "An extension's deleteProhibited function threw "
515 "an exception",
516 entry("ERROR=%s", e.what()));
517 }
518 }
519
Deepak Kodihalli33887992017-06-13 07:06:49 -0500520 // Delete the persistent representation of this error.
521 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600522 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500523 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600524
Patrick Venturef18bf832018-10-26 18:14:00 -0700525 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600526 auto it = std::find(ids.begin(), ids.end(), id);
527 if (it != ids.end())
528 {
529 ids.erase(it);
530 }
531 };
Patrick Venture34438962018-10-30 13:17:37 -0700532 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500533 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600534 removeId(infoErrors, entryId);
535 }
536 else
537 {
538 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500539 }
Patrick Venture34438962018-10-30 13:17:37 -0700540 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500541
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500542 checkAndRemoveBlockingError(entryId);
543
Matt Spinler99c2b402019-05-23 14:29:16 -0500544 for (auto& remove : Extensions::getDeleteFunctions())
545 {
546 try
547 {
548 remove(entryId);
549 }
550 catch (std::exception& e)
551 {
552 log<level::ERR>("An extension's delete function threw an "
553 "exception",
554 entry("ERROR=%s", e.what()));
555 }
556 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500557 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600558 else
559 {
560 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700561 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600562 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500563}
564
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500565void Manager::restore()
566{
Patrick Venturef18bf832018-10-26 18:14:00 -0700567 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600568 return id == restoredId;
569 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500570 std::vector<uint32_t> errorIds;
571
572 fs::path dir(ERRLOG_PERSIST_PATH);
573 if (!fs::exists(dir) || fs::is_empty(dir))
574 {
575 return;
576 }
577
Patrick Venturef18bf832018-10-26 18:14:00 -0700578 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500579 {
580 auto id = file.path().filename().c_str();
581 auto idNum = std::stol(id);
582 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700583 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500584 if (deserialize(file.path(), *e))
585 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700586 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600587 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500588 {
Adriana Kobylak1ff95ef2020-12-03 13:52:19 -0600589 e->path(file.path());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600590 e->emit_object_added();
591 if (e->severity() >= Entry::sevLowerLimit)
592 {
593 infoErrors.push_back(idNum);
594 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600595 else
596 {
597 realErrors.push_back(idNum);
598 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600599
600 entries.insert(std::make_pair(idNum, std::move(e)));
601 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500602 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600603 else
604 {
605 logging::log<logging::level::ERR>(
606 "Failed in sanity check while restoring error entry. "
607 "Ignoring error entry",
608 logging::entry("ID_NUM=%d", idNum),
609 logging::entry("ENTRY_ID=%d", e->id()));
610 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500611 }
612 }
613
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530614 if (!errorIds.empty())
615 {
616 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
617 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500618}
619
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500620void Manager::journalSync()
621{
622 bool syncRequested = false;
623 auto fd = -1;
624 auto rc = -1;
625 auto wd = -1;
626 auto bus = sdbusplus::bus::new_default();
627
628 auto start =
629 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
630 .count();
631
Matthew Barth59913892019-08-27 15:43:48 -0500632 // Each time an error log is committed, a request to sync the journal
633 // must occur and block that error log commit until it completes. A 5sec
634 // block is done to allow sufficient time for the journal to be synced.
635 //
636 // Number of loop iterations = 3 for the following reasons:
637 // Iteration #1: Requests a journal sync by killing the journald service.
638 // Iteration #2: Setup an inotify watch to monitor the synced file that
639 // journald updates with the timestamp the last time the
640 // journal was flushed.
641 // Iteration #3: Poll to wait until inotify reports an event which blocks
642 // the error log from being commited until the sync completes.
643 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500644 for (int i = 0; i < maxRetry; i++)
645 {
646 // Read timestamp from synced file
647 constexpr auto syncedPath = "/run/systemd/journal/synced";
648 std::ifstream syncedFile(syncedPath);
649 if (syncedFile.fail())
650 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500651 // If the synced file doesn't exist, a sync request will create it.
652 if (errno != ENOENT)
653 {
654 log<level::ERR>("Failed to open journal synced file",
655 entry("FILENAME=%s", syncedPath),
656 entry("ERRNO=%d", errno));
657 return;
658 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500659 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500660 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500661 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500662 // Only read the synced file if it exists.
663 // See if a sync happened by now
664 std::string timestampStr;
665 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700666 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500667 if (timestamp >= start)
668 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800669 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500670 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500671 }
672
673 // Let's ask for a sync, but only once
674 if (!syncRequested)
675 {
676 syncRequested = true;
677
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500678 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
679 auto signal = SIGRTMIN + 1;
680
681 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
682 SYSTEMD_INTERFACE, "KillUnit");
683 method.append(JOURNAL_UNIT, "main", signal);
684 bus.call(method);
685 if (method.is_method_error())
686 {
687 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800688 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500689 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500690
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500691 continue;
692 }
693
694 // Let's install the inotify watch, if we didn't do that yet. This watch
695 // monitors the syncedFile for when journald updates it with a newer
696 // timestamp. This means the journal has been flushed.
697 if (fd < 0)
698 {
699 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
700 if (fd < 0)
701 {
702 log<level::ERR>("Failed to create inotify watch",
703 entry("ERRNO=%d", errno));
704 return;
705 }
706
707 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
708 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
709 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
710 if (wd < 0)
711 {
712 log<level::ERR>("Failed to watch journal directory",
713 entry("PATH=%s", JOURNAL_RUN_PATH),
714 entry("ERRNO=%d", errno));
715 close(fd);
716 return;
717 }
718 continue;
719 }
720
721 // Let's wait until inotify reports an event
722 struct pollfd fds = {
723 .fd = fd,
724 .events = POLLIN,
Patrick Williamsf40323d2021-04-16 15:35:17 -0500725 .revents = 0,
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500726 };
727 constexpr auto pollTimeout = 5; // 5 seconds
728 rc = poll(&fds, 1, pollTimeout * 1000);
729 if (rc < 0)
730 {
731 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
732 entry("ERR=%s", strerror(-rc)));
733 inotify_rm_watch(fd, wd);
734 close(fd);
735 return;
736 }
737 else if (rc == 0)
738 {
739 log<level::INFO>("Poll timeout, no new journal synced data",
740 entry("TIMEOUT=%d", pollTimeout));
741 break;
742 }
743
744 // Read from the specified file descriptor until there is no new data,
745 // throwing away everything read since the timestamp will be read at the
746 // beginning of the loop.
747 constexpr auto maxBytes = 64;
748 uint8_t buffer[maxBytes];
749 while (read(fd, buffer, maxBytes) > 0)
750 ;
751 }
752
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500753 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500754 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500755 if (wd != -1)
756 {
757 inotify_rm_watch(fd, wd);
758 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500759 close(fd);
760 }
761
762 return;
763}
764
Matt Spinler1275bd12018-05-01 15:13:53 -0500765std::string Manager::readFWVersion()
766{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500767 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500768
Matt Spinlerf61f2922020-06-23 11:32:49 -0500769 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500770 {
771 log<level::ERR>("Unable to read BMC firmware version");
772 }
773
Matt Spinlerf61f2922020-06-23 11:32:49 -0500774 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500775}
776
Matt Spinler3fb83b32019-07-26 11:22:44 -0500777void Manager::create(const std::string& message, Entry::Level severity,
778 const std::map<std::string, std::string>& additionalData)
779{
780 // Convert the map into a vector of "key=value" strings
781 std::vector<std::string> ad;
782 metadata::associations::combine(additionalData, ad);
783
784 createEntry(message, severity, ad);
785}
786
Matt Spinlerc64b7122020-03-26 10:55:01 -0500787void Manager::createWithFFDC(
788 const std::string& message, Entry::Level severity,
789 const std::map<std::string, std::string>& additionalData,
790 const FFDCEntries& ffdc)
791{
792 // Convert the map into a vector of "key=value" strings
793 std::vector<std::string> ad;
794 metadata::associations::combine(additionalData, ad);
795
796 createEntry(message, severity, ad, ffdc);
797}
798
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500799} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600800} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700801} // namespace phosphor