blob: c8c6cc56eca580711f6bc2cce83cc0e0becaef90 [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
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050069void Manager::commit(uint64_t transactionId, std::string errMsg)
70{
71 auto level = getLevel(errMsg);
72 _commit(transactionId, std::move(errMsg), level);
73}
74
75void Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
76 uint32_t errLvl)
77{
78 _commit(transactionId, std::move(errMsg),
79 static_cast<Entry::Level>(errLvl));
80}
81
82void Manager::_commit(uint64_t transactionId, std::string&& errMsg,
83 Entry::Level errLvl)
84{
Adriana Kobylak7298dc22017-01-24 12:21:50 -060085 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -060086 // Length of 'TRANSACTION_ID' string.
Patrick Venture30047bf2018-11-01 18:52:15 -070087 constexpr const auto transactionIdVarSize = std::strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -060088 // Length of 'TRANSACTION_ID=' string.
89 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050090
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050091 // Flush all the pending log messages into the journal
92 journalSync();
Adriana Kobylakcfd9a7d2017-06-07 11:57:31 -050093
Patrick Venturef18bf832018-10-26 18:14:00 -070094 sd_journal* j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060095 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050096 if (rc < 0)
97 {
Patrick Venturef18bf832018-10-26 18:14:00 -070098 logging::log<logging::level::ERR>(
99 "Failed to open journal",
100 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600101 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500102 }
103
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600104 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -0600105 std::set<std::string> metalist;
106 auto metamap = g_errMetaMap.find(errMsg);
107 if (metamap != g_errMetaMap.end())
108 {
109 metalist.insert(metamap->second.begin(), metamap->second.end());
110 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600111
Patrick Venturef18bf832018-10-26 18:14:00 -0700112 // Add _PID field information in AdditionalData.
Jayanth Othayothdb18ebe2017-09-04 00:48:02 -0500113 metalist.insert("_PID");
114
Tom Joseph7a33ee42017-07-25 00:04:20 +0530115 std::vector<std::string> additionalData;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600116
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600117 // Read the journal from the end to get the most recent entry first.
118 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
119 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600120 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700121 const char* data = nullptr;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600122 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600123
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600124 // Look for the transaction id metadata variable
Patrick Venturef18bf832018-10-26 18:14:00 -0700125 rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data,
126 &length);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600127 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600128 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600129 // This journal entry does not have the TRANSACTION_ID
130 // metadata variable.
131 continue;
132 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600133
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600134 // journald does not guarantee that sd_journal_get_data() returns NULL
135 // terminated strings, so need to specify the size to use to compare,
136 // use the returned length instead of anything that relies on NULL
137 // terminators like strlen().
138 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
139 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
140 // 'data + transactionIdVarOffset' will be in the form of '1234'.
141 // 'length - transactionIdVarOffset' will be the length of '1234'.
142 if ((length <= (transactionIdVarOffset)) ||
Patrick Venturef18bf832018-10-26 18:14:00 -0700143 (transactionIdStr.compare(0, transactionIdStr.size(),
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600144 data + transactionIdVarOffset,
145 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600146 {
147 // The value of the TRANSACTION_ID metadata is not the requested
148 // transaction id number.
149 continue;
150 }
151
152 // Search for all metadata variables in the current journal entry.
153 for (auto i = metalist.cbegin(); i != metalist.cend();)
154 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700155 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
156 &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600157 if (rc < 0)
158 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600159 // Metadata variable not found, check next metadata variable.
160 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600161 continue;
162 }
163
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600164 // Metadata variable found, save it and remove it from the set.
165 additionalData.emplace_back(data, length);
166 i = metalist.erase(i);
167 }
168 if (metalist.empty())
169 {
170 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600171 break;
172 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600173 }
174 if (!metalist.empty())
175 {
176 // Not all the metadata variables were found in the journal.
177 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600178 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700179 logging::log<logging::level::INFO>(
180 "Failed to find metadata",
181 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600182 }
183 }
184
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500185 sd_journal_close(j);
186
Matt Spinlerb60e7552019-07-24 15:28:08 -0500187 createEntry(errMsg, errLvl, additionalData);
188}
189
190void Manager::createEntry(std::string errMsg, Entry::Level errLvl,
Matt Spinlerc64b7122020-03-26 10:55:01 -0500191 std::vector<std::string> additionalData,
192 const FFDCEntries& ffdc)
Matt Spinlerb60e7552019-07-24 15:28:08 -0500193{
194 if (!Extensions::disableDefaultLogCaps())
195 {
196 if (errLvl < Entry::sevLowerLimit)
197 {
198 if (realErrors.size() >= ERROR_CAP)
199 {
200 erase(realErrors.front());
201 }
202 }
203 else
204 {
205 if (infoErrors.size() >= ERROR_INFO_CAP)
206 {
207 erase(infoErrors.front());
208 }
209 }
210 }
211
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600212 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500213 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500214 {
215 infoErrors.push_back(entryId);
216 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600217 else
218 {
219 realErrors.push_back(entryId);
220 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600221 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700222 std::chrono::system_clock::now().time_since_epoch())
223 .count();
224 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600225
Patrick Venturef18bf832018-10-26 18:14:00 -0700226 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600227 processMetadata(errMsg, additionalData, objects);
228
Patrick Venturef18bf832018-10-26 18:14:00 -0700229 auto e = std::make_unique<Entry>(busLog, objPath, entryId,
230 ms, // Milliseconds since 1970
231 errLvl, std::move(errMsg),
232 std::move(additionalData),
233 std::move(objects), fwVersion, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500234 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500235
Andrew Geissler32874542020-07-09 09:17:03 -0500236 if (isQuiesceOnErrorEnabled() && isCalloutPresent(*e))
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500237 {
Andrew Geissler32874542020-07-09 09:17:03 -0500238 quiesceOnError(entryId);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500239 }
240
Matt Spinlerc64b7122020-03-26 10:55:01 -0500241 doExtensionLogCreate(*e, ffdc);
242
243 // Note: No need to close the file descriptors in the FFDC.
Matt Spinler99c2b402019-05-23 14:29:16 -0500244
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500245 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500246}
247
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500248bool Manager::isQuiesceOnErrorEnabled()
249{
250 std::variant<bool> property;
251
252 auto method = this->busLog.new_method_call(
253 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
254 "org.freedesktop.DBus.Properties", "Get");
255
256 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
257
258 try
259 {
260 auto reply = this->busLog.call(method);
261 reply.read(property);
262 }
263 catch (const SdBusError& e)
264 {
265 log<level::ERR>("Error reading QuiesceOnHwError property",
266 entry("ERROR=%s", e.what()));
267 throw;
268 }
269
270 return std::get<bool>(property);
271}
272
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500273bool Manager::isCalloutPresent(const Entry& entry)
274{
275 for (const auto& c : entry.additionalData())
276 {
277 if (c.find("CALLOUT_") != std::string::npos)
278 {
279 return true;
280 }
281 }
282
283 return false;
284}
285
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500286void Manager::findAndRemoveResolvedBlocks()
287{
288 for (auto& entry : entries)
289 {
290 if (entry.second->resolved())
291 {
292 checkAndRemoveBlockingError(entry.first);
293 }
294 }
295}
296
297void Manager::onEntryResolve(sdbusplus::message::message& msg)
298{
299 using Interface = std::string;
300 using Property = std::string;
301 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500302 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500303
304 Interface interface;
305 Properties properties;
306
307 msg.read(interface, properties);
308
309 for (const auto& p : properties)
310 {
311 if (p.first == "Resolved")
312 {
313 findAndRemoveResolvedBlocks();
314 return;
315 }
316 }
317}
318
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500319void Manager::checkAndQuiesceHost()
320{
321 // First check host state
322 std::variant<std::string> property;
323
324 auto method = this->busLog.new_method_call(
325 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
326 "org.freedesktop.DBus.Properties", "Get");
327
328 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
329
330 try
331 {
332 auto reply = this->busLog.call(method);
333 reply.read(property);
334 }
335 catch (const SdBusError& e)
336 {
337 // Quiescing the host is a "best effort" type function. If unable to
338 // read the host state or it comes back empty, just return.
339 // The boot block object will still be created and the associations to
340 // find the log will be present. Don't want a dependency with
341 // phosphor-state-manager service
342 log<level::INFO>("Error reading QuiesceOnHwError property",
343 entry("ERROR=%s", e.what()));
344 return;
345 }
346
347 std::string hostState = std::get<std::string>(property);
348
349 // If host state is empty, do nothing
350 if (hostState.empty())
351 {
352 return;
353 }
354
355 using Host = sdbusplus::xyz::openbmc_project::State::server::Host;
356 auto state = Host::convertHostStateFromString(hostState);
357 if (state != Host::HostState::Running)
358 {
359 return;
360 }
361
362 auto quiesce = this->busLog.new_method_call(
363 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
364 "org.freedesktop.systemd1.Manager", "StartUnit");
365
366 quiesce.append("obmc-host-quiesce@0.target");
367 quiesce.append("replace");
368
369 this->busLog.call_noreply(quiesce);
370}
371
Andrew Geissler32874542020-07-09 09:17:03 -0500372void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500373{
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500374
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500375 logging::log<logging::level::INFO>(
376 "QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500377
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500378 auto blockPath =
Andrew Geissler32874542020-07-09 09:17:03 -0500379 std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId);
380 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500381 this->blockingErrors.push_back(std::move(blockObj));
382
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500383 // Register call back if log is resolved
384 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500385 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500386 auto callback = std::make_unique<sdbusplus::bus::match::match>(
387 this->busLog,
388 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
389 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
390 std::placeholders::_1));
391
392 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500393 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500394
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500395 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500396}
397
Matt Spinlerc64b7122020-03-26 10:55:01 -0500398void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500399{
400 // Make the association <endpointpath>/<endpointtype> paths
401 std::vector<std::string> assocs;
402 for (const auto& [forwardType, reverseType, endpoint] :
403 entry.associations())
404 {
405 std::string e{endpoint};
406 e += '/' + reverseType;
407 assocs.push_back(e);
408 }
409
410 for (auto& create : Extensions::getCreateFunctions())
411 {
412 try
413 {
414 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500415 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500416 }
417 catch (std::exception& e)
418 {
419 log<level::ERR>("An extension's create function threw an exception",
420 phosphor::logging::entry("ERROR=%s", e.what()));
421 }
422 }
423}
424
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600425void Manager::processMetadata(const std::string& errorName,
426 const std::vector<std::string>& additionalData,
427 AssociationList& objects) const
428{
429 // additionalData is a list of "metadata=value"
430 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700431 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600432 {
Patrick Venture34438962018-10-30 13:17:37 -0700433 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700434 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600435 {
Patrick Venture34438962018-10-30 13:17:37 -0700436 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600437 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700438 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600439 {
440 (iter->second)(metadata, additionalData, objects);
441 }
442 }
443 }
444}
445
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500446void Manager::checkAndRemoveBlockingError(uint32_t entryId)
447{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500448 // First look for blocking object and remove
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500449 auto it = find_if(
450 blockingErrors.begin(), blockingErrors.end(),
451 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
452 if (it != blockingErrors.end())
453 {
454 blockingErrors.erase(it);
455 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500456
457 // Now remove the callback looking for the error to be resolved
458 auto resolveFind = propChangedEntryCallback.find(entryId);
459 if (resolveFind != propChangedEntryCallback.end())
460 {
461 propChangedEntryCallback.erase(resolveFind);
462 }
463
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500464 return;
465}
466
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500467void Manager::erase(uint32_t entryId)
468{
Patrick Venture34438962018-10-30 13:17:37 -0700469 auto entryFound = entries.find(entryId);
470 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500471 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500472 for (auto& func : Extensions::getDeleteProhibitedFunctions())
473 {
474 try
475 {
476 bool prohibited = false;
477 func(entryId, prohibited);
478 if (prohibited)
479 {
480 // Future work remains to throw an error here.
481 return;
482 }
483 }
484 catch (std::exception& e)
485 {
486 log<level::ERR>(
487 "An extension's deleteProhibited function threw "
488 "an exception",
489 entry("ERROR=%s", e.what()));
490 }
491 }
492
Deepak Kodihalli33887992017-06-13 07:06:49 -0500493 // Delete the persistent representation of this error.
494 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600495 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500496 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600497
Patrick Venturef18bf832018-10-26 18:14:00 -0700498 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600499 auto it = std::find(ids.begin(), ids.end(), id);
500 if (it != ids.end())
501 {
502 ids.erase(it);
503 }
504 };
Patrick Venture34438962018-10-30 13:17:37 -0700505 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500506 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600507 removeId(infoErrors, entryId);
508 }
509 else
510 {
511 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500512 }
Patrick Venture34438962018-10-30 13:17:37 -0700513 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500514
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500515 checkAndRemoveBlockingError(entryId);
516
Matt Spinler99c2b402019-05-23 14:29:16 -0500517 for (auto& remove : Extensions::getDeleteFunctions())
518 {
519 try
520 {
521 remove(entryId);
522 }
523 catch (std::exception& e)
524 {
525 log<level::ERR>("An extension's delete function threw an "
526 "exception",
527 entry("ERROR=%s", e.what()));
528 }
529 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500530 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600531 else
532 {
533 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700534 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600535 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500536}
537
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500538void Manager::restore()
539{
Patrick Venturef18bf832018-10-26 18:14:00 -0700540 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600541 return id == restoredId;
542 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500543 std::vector<uint32_t> errorIds;
544
545 fs::path dir(ERRLOG_PERSIST_PATH);
546 if (!fs::exists(dir) || fs::is_empty(dir))
547 {
548 return;
549 }
550
Patrick Venturef18bf832018-10-26 18:14:00 -0700551 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500552 {
553 auto id = file.path().filename().c_str();
554 auto idNum = std::stol(id);
555 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700556 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500557 if (deserialize(file.path(), *e))
558 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700559 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600560 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500561 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600562 e->emit_object_added();
563 if (e->severity() >= Entry::sevLowerLimit)
564 {
565 infoErrors.push_back(idNum);
566 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600567 else
568 {
569 realErrors.push_back(idNum);
570 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600571
572 entries.insert(std::make_pair(idNum, std::move(e)));
573 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500574 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600575 else
576 {
577 logging::log<logging::level::ERR>(
578 "Failed in sanity check while restoring error entry. "
579 "Ignoring error entry",
580 logging::entry("ID_NUM=%d", idNum),
581 logging::entry("ENTRY_ID=%d", e->id()));
582 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500583 }
584 }
585
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530586 if (!errorIds.empty())
587 {
588 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
589 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500590}
591
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500592void Manager::journalSync()
593{
594 bool syncRequested = false;
595 auto fd = -1;
596 auto rc = -1;
597 auto wd = -1;
598 auto bus = sdbusplus::bus::new_default();
599
600 auto start =
601 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
602 .count();
603
Matthew Barth59913892019-08-27 15:43:48 -0500604 // Each time an error log is committed, a request to sync the journal
605 // must occur and block that error log commit until it completes. A 5sec
606 // block is done to allow sufficient time for the journal to be synced.
607 //
608 // Number of loop iterations = 3 for the following reasons:
609 // Iteration #1: Requests a journal sync by killing the journald service.
610 // Iteration #2: Setup an inotify watch to monitor the synced file that
611 // journald updates with the timestamp the last time the
612 // journal was flushed.
613 // Iteration #3: Poll to wait until inotify reports an event which blocks
614 // the error log from being commited until the sync completes.
615 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500616 for (int i = 0; i < maxRetry; i++)
617 {
618 // Read timestamp from synced file
619 constexpr auto syncedPath = "/run/systemd/journal/synced";
620 std::ifstream syncedFile(syncedPath);
621 if (syncedFile.fail())
622 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500623 // If the synced file doesn't exist, a sync request will create it.
624 if (errno != ENOENT)
625 {
626 log<level::ERR>("Failed to open journal synced file",
627 entry("FILENAME=%s", syncedPath),
628 entry("ERRNO=%d", errno));
629 return;
630 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500631 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500632 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500633 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500634 // Only read the synced file if it exists.
635 // See if a sync happened by now
636 std::string timestampStr;
637 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700638 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500639 if (timestamp >= start)
640 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800641 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500642 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500643 }
644
645 // Let's ask for a sync, but only once
646 if (!syncRequested)
647 {
648 syncRequested = true;
649
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500650 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
651 auto signal = SIGRTMIN + 1;
652
653 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
654 SYSTEMD_INTERFACE, "KillUnit");
655 method.append(JOURNAL_UNIT, "main", signal);
656 bus.call(method);
657 if (method.is_method_error())
658 {
659 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800660 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500661 }
662 continue;
663 }
664
665 // Let's install the inotify watch, if we didn't do that yet. This watch
666 // monitors the syncedFile for when journald updates it with a newer
667 // timestamp. This means the journal has been flushed.
668 if (fd < 0)
669 {
670 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
671 if (fd < 0)
672 {
673 log<level::ERR>("Failed to create inotify watch",
674 entry("ERRNO=%d", errno));
675 return;
676 }
677
678 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
679 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
680 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
681 if (wd < 0)
682 {
683 log<level::ERR>("Failed to watch journal directory",
684 entry("PATH=%s", JOURNAL_RUN_PATH),
685 entry("ERRNO=%d", errno));
686 close(fd);
687 return;
688 }
689 continue;
690 }
691
692 // Let's wait until inotify reports an event
693 struct pollfd fds = {
694 .fd = fd,
695 .events = POLLIN,
696 };
697 constexpr auto pollTimeout = 5; // 5 seconds
698 rc = poll(&fds, 1, pollTimeout * 1000);
699 if (rc < 0)
700 {
701 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
702 entry("ERR=%s", strerror(-rc)));
703 inotify_rm_watch(fd, wd);
704 close(fd);
705 return;
706 }
707 else if (rc == 0)
708 {
709 log<level::INFO>("Poll timeout, no new journal synced data",
710 entry("TIMEOUT=%d", pollTimeout));
711 break;
712 }
713
714 // Read from the specified file descriptor until there is no new data,
715 // throwing away everything read since the timestamp will be read at the
716 // beginning of the loop.
717 constexpr auto maxBytes = 64;
718 uint8_t buffer[maxBytes];
719 while (read(fd, buffer, maxBytes) > 0)
720 ;
721 }
722
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500723 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500724 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500725 if (wd != -1)
726 {
727 inotify_rm_watch(fd, wd);
728 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500729 close(fd);
730 }
731
732 return;
733}
734
Matt Spinler1275bd12018-05-01 15:13:53 -0500735std::string Manager::readFWVersion()
736{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500737 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500738
Matt Spinlerf61f2922020-06-23 11:32:49 -0500739 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500740 {
741 log<level::ERR>("Unable to read BMC firmware version");
742 }
743
Matt Spinlerf61f2922020-06-23 11:32:49 -0500744 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500745}
746
Matt Spinler3fb83b32019-07-26 11:22:44 -0500747void Manager::create(const std::string& message, Entry::Level severity,
748 const std::map<std::string, std::string>& additionalData)
749{
750 // Convert the map into a vector of "key=value" strings
751 std::vector<std::string> ad;
752 metadata::associations::combine(additionalData, ad);
753
754 createEntry(message, severity, ad);
755}
756
Matt Spinlerc64b7122020-03-26 10:55:01 -0500757void Manager::createWithFFDC(
758 const std::string& message, Entry::Level severity,
759 const std::map<std::string, std::string>& additionalData,
760 const FFDCEntries& ffdc)
761{
762 // Convert the map into a vector of "key=value" strings
763 std::vector<std::string> ad;
764 metadata::associations::combine(additionalData, ad);
765
766 createEntry(message, severity, ad, ffdc);
767}
768
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500769} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600770} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700771} // namespace phosphor