blob: 98be0d19c3f7ac9fc3d066ba36586e092bba33b1 [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);
Adriana Kobylak1ff95ef2020-12-03 13:52:19 -0600234 auto path = serialize(*e);
235 e->path(path);
Matt Spinler99c2b402019-05-23 14:29:16 -0500236
Andrew Geissler32874542020-07-09 09:17:03 -0500237 if (isQuiesceOnErrorEnabled() && isCalloutPresent(*e))
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500238 {
Andrew Geissler32874542020-07-09 09:17:03 -0500239 quiesceOnError(entryId);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500240 }
241
Matt Spinlerc64b7122020-03-26 10:55:01 -0500242 doExtensionLogCreate(*e, ffdc);
243
244 // Note: No need to close the file descriptors in the FFDC.
Matt Spinler99c2b402019-05-23 14:29:16 -0500245
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500246 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500247}
248
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500249bool Manager::isQuiesceOnErrorEnabled()
250{
251 std::variant<bool> property;
252
253 auto method = this->busLog.new_method_call(
254 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
255 "org.freedesktop.DBus.Properties", "Get");
256
257 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
258
259 try
260 {
261 auto reply = this->busLog.call(method);
262 reply.read(property);
263 }
264 catch (const SdBusError& e)
265 {
266 log<level::ERR>("Error reading QuiesceOnHwError property",
267 entry("ERROR=%s", e.what()));
268 throw;
269 }
270
271 return std::get<bool>(property);
272}
273
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500274bool Manager::isCalloutPresent(const Entry& entry)
275{
276 for (const auto& c : entry.additionalData())
277 {
278 if (c.find("CALLOUT_") != std::string::npos)
279 {
280 return true;
281 }
282 }
283
284 return false;
285}
286
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500287void Manager::findAndRemoveResolvedBlocks()
288{
289 for (auto& entry : entries)
290 {
291 if (entry.second->resolved())
292 {
293 checkAndRemoveBlockingError(entry.first);
294 }
295 }
296}
297
298void Manager::onEntryResolve(sdbusplus::message::message& msg)
299{
300 using Interface = std::string;
301 using Property = std::string;
302 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500303 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500304
305 Interface interface;
306 Properties properties;
307
308 msg.read(interface, properties);
309
310 for (const auto& p : properties)
311 {
312 if (p.first == "Resolved")
313 {
314 findAndRemoveResolvedBlocks();
315 return;
316 }
317 }
318}
319
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500320void Manager::checkAndQuiesceHost()
321{
322 // First check host state
323 std::variant<std::string> property;
324
325 auto method = this->busLog.new_method_call(
326 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
327 "org.freedesktop.DBus.Properties", "Get");
328
329 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
330
331 try
332 {
333 auto reply = this->busLog.call(method);
334 reply.read(property);
335 }
336 catch (const SdBusError& e)
337 {
338 // Quiescing the host is a "best effort" type function. If unable to
339 // read the host state or it comes back empty, just return.
340 // The boot block object will still be created and the associations to
341 // find the log will be present. Don't want a dependency with
342 // phosphor-state-manager service
343 log<level::INFO>("Error reading QuiesceOnHwError property",
344 entry("ERROR=%s", e.what()));
345 return;
346 }
347
348 std::string hostState = std::get<std::string>(property);
349
350 // If host state is empty, do nothing
351 if (hostState.empty())
352 {
353 return;
354 }
355
356 using Host = sdbusplus::xyz::openbmc_project::State::server::Host;
357 auto state = Host::convertHostStateFromString(hostState);
358 if (state != Host::HostState::Running)
359 {
360 return;
361 }
362
363 auto quiesce = this->busLog.new_method_call(
364 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
365 "org.freedesktop.systemd1.Manager", "StartUnit");
366
367 quiesce.append("obmc-host-quiesce@0.target");
368 quiesce.append("replace");
369
370 this->busLog.call_noreply(quiesce);
371}
372
Andrew Geissler32874542020-07-09 09:17:03 -0500373void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500374{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500375 // Verify we don't already have this entry blocking
376 auto it = find_if(
377 this->blockingErrors.begin(), this->blockingErrors.end(),
378 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
379 if (it != this->blockingErrors.end())
380 {
381 // Already recorded so just return
382 logging::log<logging::level::DEBUG>(
383 "QuiesceOnError set and callout present but entry already logged");
384 return;
385 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500386
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500387 logging::log<logging::level::INFO>(
388 "QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500389
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500390 auto blockPath =
Andrew Geissler32874542020-07-09 09:17:03 -0500391 std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId);
392 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500393 this->blockingErrors.push_back(std::move(blockObj));
394
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500395 // Register call back if log is resolved
396 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500397 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500398 auto callback = std::make_unique<sdbusplus::bus::match::match>(
399 this->busLog,
400 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
401 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
402 std::placeholders::_1));
403
404 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500405 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500406
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500407 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500408}
409
Matt Spinlerc64b7122020-03-26 10:55:01 -0500410void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500411{
412 // Make the association <endpointpath>/<endpointtype> paths
413 std::vector<std::string> assocs;
414 for (const auto& [forwardType, reverseType, endpoint] :
415 entry.associations())
416 {
417 std::string e{endpoint};
418 e += '/' + reverseType;
419 assocs.push_back(e);
420 }
421
422 for (auto& create : Extensions::getCreateFunctions())
423 {
424 try
425 {
426 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500427 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500428 }
429 catch (std::exception& e)
430 {
431 log<level::ERR>("An extension's create function threw an exception",
432 phosphor::logging::entry("ERROR=%s", e.what()));
433 }
434 }
435}
436
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600437void Manager::processMetadata(const std::string& errorName,
438 const std::vector<std::string>& additionalData,
439 AssociationList& objects) const
440{
441 // additionalData is a list of "metadata=value"
442 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700443 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600444 {
Patrick Venture34438962018-10-30 13:17:37 -0700445 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700446 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600447 {
Patrick Venture34438962018-10-30 13:17:37 -0700448 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600449 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700450 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600451 {
452 (iter->second)(metadata, additionalData, objects);
453 }
454 }
455 }
456}
457
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500458void Manager::checkAndRemoveBlockingError(uint32_t entryId)
459{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500460 // First look for blocking object and remove
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500461 auto it = find_if(
462 blockingErrors.begin(), blockingErrors.end(),
463 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
464 if (it != blockingErrors.end())
465 {
466 blockingErrors.erase(it);
467 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500468
469 // Now remove the callback looking for the error to be resolved
470 auto resolveFind = propChangedEntryCallback.find(entryId);
471 if (resolveFind != propChangedEntryCallback.end())
472 {
473 propChangedEntryCallback.erase(resolveFind);
474 }
475
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500476 return;
477}
478
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500479void Manager::erase(uint32_t entryId)
480{
Patrick Venture34438962018-10-30 13:17:37 -0700481 auto entryFound = entries.find(entryId);
482 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500483 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500484 for (auto& func : Extensions::getDeleteProhibitedFunctions())
485 {
486 try
487 {
488 bool prohibited = false;
489 func(entryId, prohibited);
490 if (prohibited)
491 {
492 // Future work remains to throw an error here.
493 return;
494 }
495 }
496 catch (std::exception& e)
497 {
498 log<level::ERR>(
499 "An extension's deleteProhibited function threw "
500 "an exception",
501 entry("ERROR=%s", e.what()));
502 }
503 }
504
Deepak Kodihalli33887992017-06-13 07:06:49 -0500505 // Delete the persistent representation of this error.
506 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600507 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500508 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600509
Patrick Venturef18bf832018-10-26 18:14:00 -0700510 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600511 auto it = std::find(ids.begin(), ids.end(), id);
512 if (it != ids.end())
513 {
514 ids.erase(it);
515 }
516 };
Patrick Venture34438962018-10-30 13:17:37 -0700517 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500518 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600519 removeId(infoErrors, entryId);
520 }
521 else
522 {
523 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500524 }
Patrick Venture34438962018-10-30 13:17:37 -0700525 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500526
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500527 checkAndRemoveBlockingError(entryId);
528
Matt Spinler99c2b402019-05-23 14:29:16 -0500529 for (auto& remove : Extensions::getDeleteFunctions())
530 {
531 try
532 {
533 remove(entryId);
534 }
535 catch (std::exception& e)
536 {
537 log<level::ERR>("An extension's delete function threw an "
538 "exception",
539 entry("ERROR=%s", e.what()));
540 }
541 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500542 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600543 else
544 {
545 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700546 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600547 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500548}
549
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500550void Manager::restore()
551{
Patrick Venturef18bf832018-10-26 18:14:00 -0700552 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600553 return id == restoredId;
554 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500555 std::vector<uint32_t> errorIds;
556
557 fs::path dir(ERRLOG_PERSIST_PATH);
558 if (!fs::exists(dir) || fs::is_empty(dir))
559 {
560 return;
561 }
562
Patrick Venturef18bf832018-10-26 18:14:00 -0700563 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500564 {
565 auto id = file.path().filename().c_str();
566 auto idNum = std::stol(id);
567 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700568 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500569 if (deserialize(file.path(), *e))
570 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700571 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600572 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500573 {
Adriana Kobylak1ff95ef2020-12-03 13:52:19 -0600574 e->path(file.path());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600575 e->emit_object_added();
576 if (e->severity() >= Entry::sevLowerLimit)
577 {
578 infoErrors.push_back(idNum);
579 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600580 else
581 {
582 realErrors.push_back(idNum);
583 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600584
585 entries.insert(std::make_pair(idNum, std::move(e)));
586 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500587 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600588 else
589 {
590 logging::log<logging::level::ERR>(
591 "Failed in sanity check while restoring error entry. "
592 "Ignoring error entry",
593 logging::entry("ID_NUM=%d", idNum),
594 logging::entry("ENTRY_ID=%d", e->id()));
595 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500596 }
597 }
598
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530599 if (!errorIds.empty())
600 {
601 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
602 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500603}
604
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500605void Manager::journalSync()
606{
607 bool syncRequested = false;
608 auto fd = -1;
609 auto rc = -1;
610 auto wd = -1;
611 auto bus = sdbusplus::bus::new_default();
612
613 auto start =
614 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
615 .count();
616
Matthew Barth59913892019-08-27 15:43:48 -0500617 // Each time an error log is committed, a request to sync the journal
618 // must occur and block that error log commit until it completes. A 5sec
619 // block is done to allow sufficient time for the journal to be synced.
620 //
621 // Number of loop iterations = 3 for the following reasons:
622 // Iteration #1: Requests a journal sync by killing the journald service.
623 // Iteration #2: Setup an inotify watch to monitor the synced file that
624 // journald updates with the timestamp the last time the
625 // journal was flushed.
626 // Iteration #3: Poll to wait until inotify reports an event which blocks
627 // the error log from being commited until the sync completes.
628 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500629 for (int i = 0; i < maxRetry; i++)
630 {
631 // Read timestamp from synced file
632 constexpr auto syncedPath = "/run/systemd/journal/synced";
633 std::ifstream syncedFile(syncedPath);
634 if (syncedFile.fail())
635 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500636 // If the synced file doesn't exist, a sync request will create it.
637 if (errno != ENOENT)
638 {
639 log<level::ERR>("Failed to open journal synced file",
640 entry("FILENAME=%s", syncedPath),
641 entry("ERRNO=%d", errno));
642 return;
643 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500644 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500645 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500646 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500647 // Only read the synced file if it exists.
648 // See if a sync happened by now
649 std::string timestampStr;
650 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700651 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500652 if (timestamp >= start)
653 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800654 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500655 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500656 }
657
658 // Let's ask for a sync, but only once
659 if (!syncRequested)
660 {
661 syncRequested = true;
662
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500663 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
664 auto signal = SIGRTMIN + 1;
665
666 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
667 SYSTEMD_INTERFACE, "KillUnit");
668 method.append(JOURNAL_UNIT, "main", signal);
669 bus.call(method);
670 if (method.is_method_error())
671 {
672 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800673 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500674 }
675 continue;
676 }
677
678 // Let's install the inotify watch, if we didn't do that yet. This watch
679 // monitors the syncedFile for when journald updates it with a newer
680 // timestamp. This means the journal has been flushed.
681 if (fd < 0)
682 {
683 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
684 if (fd < 0)
685 {
686 log<level::ERR>("Failed to create inotify watch",
687 entry("ERRNO=%d", errno));
688 return;
689 }
690
691 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
692 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
693 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
694 if (wd < 0)
695 {
696 log<level::ERR>("Failed to watch journal directory",
697 entry("PATH=%s", JOURNAL_RUN_PATH),
698 entry("ERRNO=%d", errno));
699 close(fd);
700 return;
701 }
702 continue;
703 }
704
705 // Let's wait until inotify reports an event
706 struct pollfd fds = {
707 .fd = fd,
708 .events = POLLIN,
709 };
710 constexpr auto pollTimeout = 5; // 5 seconds
711 rc = poll(&fds, 1, pollTimeout * 1000);
712 if (rc < 0)
713 {
714 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
715 entry("ERR=%s", strerror(-rc)));
716 inotify_rm_watch(fd, wd);
717 close(fd);
718 return;
719 }
720 else if (rc == 0)
721 {
722 log<level::INFO>("Poll timeout, no new journal synced data",
723 entry("TIMEOUT=%d", pollTimeout));
724 break;
725 }
726
727 // Read from the specified file descriptor until there is no new data,
728 // throwing away everything read since the timestamp will be read at the
729 // beginning of the loop.
730 constexpr auto maxBytes = 64;
731 uint8_t buffer[maxBytes];
732 while (read(fd, buffer, maxBytes) > 0)
733 ;
734 }
735
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500736 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500737 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500738 if (wd != -1)
739 {
740 inotify_rm_watch(fd, wd);
741 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500742 close(fd);
743 }
744
745 return;
746}
747
Matt Spinler1275bd12018-05-01 15:13:53 -0500748std::string Manager::readFWVersion()
749{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500750 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500751
Matt Spinlerf61f2922020-06-23 11:32:49 -0500752 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500753 {
754 log<level::ERR>("Unable to read BMC firmware version");
755 }
756
Matt Spinlerf61f2922020-06-23 11:32:49 -0500757 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500758}
759
Matt Spinler3fb83b32019-07-26 11:22:44 -0500760void Manager::create(const std::string& message, Entry::Level severity,
761 const std::map<std::string, std::string>& additionalData)
762{
763 // Convert the map into a vector of "key=value" strings
764 std::vector<std::string> ad;
765 metadata::associations::combine(additionalData, ad);
766
767 createEntry(message, severity, ad);
768}
769
Matt Spinlerc64b7122020-03-26 10:55:01 -0500770void Manager::createWithFFDC(
771 const std::string& message, Entry::Level severity,
772 const std::map<std::string, std::string>& additionalData,
773 const FFDCEntries& ffdc)
774{
775 // Convert the map into a vector of "key=value" strings
776 std::vector<std::string> ad;
777 metadata::associations::combine(additionalData, ad);
778
779 createEntry(message, severity, ad, ffdc);
780}
781
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500782} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600783} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700784} // namespace phosphor