blob: cbfe434bdba9df2bcbde7a6aa7cc846b0ba53b7d [file] [log] [blame]
Patrick Venturef18bf832018-10-26 18:14:00 -07001#include "config.h"
2
3#include "log_manager.hpp"
4
5#include "elog_entry.hpp"
6#include "elog_meta.hpp"
7#include "elog_serialize.hpp"
Matt Spinler99c2b402019-05-23 14:29:16 -05008#include "extensions.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -07009
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050010#include <poll.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050011#include <sys/inotify.h>
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050012#include <systemd/sd-bus.h>
Adriana Kobylakd311bc82016-10-16 09:54:40 -050013#include <systemd/sd-journal.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050014#include <unistd.h>
Patrick Venturef18bf832018-10-26 18:14:00 -070015
Matt Spinler99c2b402019-05-23 14:29:16 -050016#include <cassert>
Patrick Venturef18bf832018-10-26 18:14:00 -070017#include <chrono>
18#include <cstdio>
Patrick Venture30047bf2018-11-01 18:52:15 -070019#include <cstring>
Patrick Venturef18bf832018-10-26 18:14:00 -070020#include <fstream>
Patrick Venture30047bf2018-11-01 18:52:15 -070021#include <functional>
Patrick Venturef18bf832018-10-26 18:14:00 -070022#include <future>
23#include <iostream>
Patrick Venture30047bf2018-11-01 18:52:15 -070024#include <map>
Saqib Khan2bb15192017-02-13 13:19:55 -060025#include <phosphor-logging/log.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -070026#include <sdbusplus/vtable.hpp>
27#include <set>
28#include <string>
29#include <vector>
Andrew Geisslerf6126a72020-05-08 10:41:09 -050030#include <xyz/openbmc_project/State/Host/server.hpp>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060031
32using namespace phosphor::logging;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050033using namespace std::chrono;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -050034using sdbusplus::exception::SdBusError;
Deepak Kodihallia87c1572017-02-28 07:40:34 -060035extern const std::map<metadata::Metadata,
Patrick Venturef18bf832018-10-26 18:14:00 -070036 std::function<metadata::associations::Type>>
37 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050038
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060039namespace phosphor
40{
41namespace logging
42{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050043namespace internal
44{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050045inline auto getLevel(const std::string& errMsg)
46{
47 auto reqLevel = Entry::Level::Error; // Default to Error
48
49 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050050 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050051 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050052 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050053 }
54
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050055 return reqLevel;
56}
57
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050058int Manager::getRealErrSize()
59{
60 return realErrors.size();
61}
62
63int Manager::getInfoErrSize()
64{
65 return infoErrors.size();
66}
67
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050068void Manager::commit(uint64_t transactionId, std::string errMsg)
69{
70 auto level = getLevel(errMsg);
71 _commit(transactionId, std::move(errMsg), level);
72}
73
74void Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
75 uint32_t errLvl)
76{
77 _commit(transactionId, std::move(errMsg),
78 static_cast<Entry::Level>(errLvl));
79}
80
81void Manager::_commit(uint64_t transactionId, std::string&& errMsg,
82 Entry::Level errLvl)
83{
Adriana Kobylak7298dc22017-01-24 12:21:50 -060084 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -060085 // Length of 'TRANSACTION_ID' string.
Patrick Venture30047bf2018-11-01 18:52:15 -070086 constexpr const auto transactionIdVarSize = std::strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -060087 // Length of 'TRANSACTION_ID=' string.
88 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050089
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050090 // Flush all the pending log messages into the journal
91 journalSync();
Adriana Kobylakcfd9a7d2017-06-07 11:57:31 -050092
Patrick Venturef18bf832018-10-26 18:14:00 -070093 sd_journal* j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060094 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050095 if (rc < 0)
96 {
Patrick Venturef18bf832018-10-26 18:14:00 -070097 logging::log<logging::level::ERR>(
98 "Failed to open journal",
99 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600100 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500101 }
102
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600103 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -0600104 std::set<std::string> metalist;
105 auto metamap = g_errMetaMap.find(errMsg);
106 if (metamap != g_errMetaMap.end())
107 {
108 metalist.insert(metamap->second.begin(), metamap->second.end());
109 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600110
Patrick Venturef18bf832018-10-26 18:14:00 -0700111 // Add _PID field information in AdditionalData.
Jayanth Othayothdb18ebe2017-09-04 00:48:02 -0500112 metalist.insert("_PID");
113
Tom Joseph7a33ee42017-07-25 00:04:20 +0530114 std::vector<std::string> additionalData;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600115
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600116 // Read the journal from the end to get the most recent entry first.
117 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
118 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600119 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700120 const char* data = nullptr;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600121 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600122
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600123 // Look for the transaction id metadata variable
Patrick Venturef18bf832018-10-26 18:14:00 -0700124 rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data,
125 &length);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600126 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600127 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600128 // This journal entry does not have the TRANSACTION_ID
129 // metadata variable.
130 continue;
131 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600132
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600133 // journald does not guarantee that sd_journal_get_data() returns NULL
134 // terminated strings, so need to specify the size to use to compare,
135 // use the returned length instead of anything that relies on NULL
136 // terminators like strlen().
137 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
138 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
139 // 'data + transactionIdVarOffset' will be in the form of '1234'.
140 // 'length - transactionIdVarOffset' will be the length of '1234'.
141 if ((length <= (transactionIdVarOffset)) ||
Patrick Venturef18bf832018-10-26 18:14:00 -0700142 (transactionIdStr.compare(0, transactionIdStr.size(),
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600143 data + transactionIdVarOffset,
144 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600145 {
146 // The value of the TRANSACTION_ID metadata is not the requested
147 // transaction id number.
148 continue;
149 }
150
151 // Search for all metadata variables in the current journal entry.
152 for (auto i = metalist.cbegin(); i != metalist.cend();)
153 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700154 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
155 &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600156 if (rc < 0)
157 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600158 // Metadata variable not found, check next metadata variable.
159 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600160 continue;
161 }
162
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600163 // Metadata variable found, save it and remove it from the set.
164 additionalData.emplace_back(data, length);
165 i = metalist.erase(i);
166 }
167 if (metalist.empty())
168 {
169 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600170 break;
171 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600172 }
173 if (!metalist.empty())
174 {
175 // Not all the metadata variables were found in the journal.
176 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600177 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700178 logging::log<logging::level::INFO>(
179 "Failed to find metadata",
180 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600181 }
182 }
183
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500184 sd_journal_close(j);
185
Matt Spinlerb60e7552019-07-24 15:28:08 -0500186 createEntry(errMsg, errLvl, additionalData);
187}
188
189void Manager::createEntry(std::string errMsg, Entry::Level errLvl,
Matt Spinlerc64b7122020-03-26 10:55:01 -0500190 std::vector<std::string> additionalData,
191 const FFDCEntries& ffdc)
Matt Spinlerb60e7552019-07-24 15:28:08 -0500192{
193 if (!Extensions::disableDefaultLogCaps())
194 {
195 if (errLvl < Entry::sevLowerLimit)
196 {
197 if (realErrors.size() >= ERROR_CAP)
198 {
199 erase(realErrors.front());
200 }
201 }
202 else
203 {
204 if (infoErrors.size() >= ERROR_INFO_CAP)
205 {
206 erase(infoErrors.front());
207 }
208 }
209 }
210
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600211 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500212 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500213 {
214 infoErrors.push_back(entryId);
215 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600216 else
217 {
218 realErrors.push_back(entryId);
219 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600220 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700221 std::chrono::system_clock::now().time_since_epoch())
222 .count();
223 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600224
Patrick Venturef18bf832018-10-26 18:14:00 -0700225 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600226 processMetadata(errMsg, additionalData, objects);
227
Patrick Venturef18bf832018-10-26 18:14:00 -0700228 auto e = std::make_unique<Entry>(busLog, objPath, entryId,
229 ms, // Milliseconds since 1970
230 errLvl, std::move(errMsg),
231 std::move(additionalData),
232 std::move(objects), fwVersion, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500233 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500234
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500235 if (isQuiesceOnErrorEnabled())
236 {
237 checkQuiesceOnError(*e);
238 }
239
Matt Spinlerc64b7122020-03-26 10:55:01 -0500240 doExtensionLogCreate(*e, ffdc);
241
242 // Note: No need to close the file descriptors in the FFDC.
Matt Spinler99c2b402019-05-23 14:29:16 -0500243
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500244 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500245}
246
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500247bool Manager::isQuiesceOnErrorEnabled()
248{
249 std::variant<bool> property;
250
251 auto method = this->busLog.new_method_call(
252 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
253 "org.freedesktop.DBus.Properties", "Get");
254
255 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
256
257 try
258 {
259 auto reply = this->busLog.call(method);
260 reply.read(property);
261 }
262 catch (const SdBusError& e)
263 {
264 log<level::ERR>("Error reading QuiesceOnHwError property",
265 entry("ERROR=%s", e.what()));
266 throw;
267 }
268
269 return std::get<bool>(property);
270}
271
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500272bool Manager::isCalloutPresent(const Entry& entry)
273{
274 for (const auto& c : entry.additionalData())
275 {
276 if (c.find("CALLOUT_") != std::string::npos)
277 {
278 return true;
279 }
280 }
281
282 return false;
283}
284
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500285void Manager::findAndRemoveResolvedBlocks()
286{
287 for (auto& entry : entries)
288 {
289 if (entry.second->resolved())
290 {
291 checkAndRemoveBlockingError(entry.first);
292 }
293 }
294}
295
296void Manager::onEntryResolve(sdbusplus::message::message& msg)
297{
298 using Interface = std::string;
299 using Property = std::string;
300 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500301 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500302
303 Interface interface;
304 Properties properties;
305
306 msg.read(interface, properties);
307
308 for (const auto& p : properties)
309 {
310 if (p.first == "Resolved")
311 {
312 findAndRemoveResolvedBlocks();
313 return;
314 }
315 }
316}
317
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500318void Manager::checkAndQuiesceHost()
319{
320 // First check host state
321 std::variant<std::string> property;
322
323 auto method = this->busLog.new_method_call(
324 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
325 "org.freedesktop.DBus.Properties", "Get");
326
327 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
328
329 try
330 {
331 auto reply = this->busLog.call(method);
332 reply.read(property);
333 }
334 catch (const SdBusError& e)
335 {
336 // Quiescing the host is a "best effort" type function. If unable to
337 // read the host state or it comes back empty, just return.
338 // The boot block object will still be created and the associations to
339 // find the log will be present. Don't want a dependency with
340 // phosphor-state-manager service
341 log<level::INFO>("Error reading QuiesceOnHwError property",
342 entry("ERROR=%s", e.what()));
343 return;
344 }
345
346 std::string hostState = std::get<std::string>(property);
347
348 // If host state is empty, do nothing
349 if (hostState.empty())
350 {
351 return;
352 }
353
354 using Host = sdbusplus::xyz::openbmc_project::State::server::Host;
355 auto state = Host::convertHostStateFromString(hostState);
356 if (state != Host::HostState::Running)
357 {
358 return;
359 }
360
361 auto quiesce = this->busLog.new_method_call(
362 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
363 "org.freedesktop.systemd1.Manager", "StartUnit");
364
365 quiesce.append("obmc-host-quiesce@0.target");
366 quiesce.append("replace");
367
368 this->busLog.call_noreply(quiesce);
369}
370
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500371void Manager::checkQuiesceOnError(const Entry& entry)
372{
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500373
374 if (!isCalloutPresent(entry))
375 {
376 return;
377 }
378
379 logging::log<logging::level::INFO>(
380 "QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500381
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500382 auto blockPath =
383 std::string(OBJ_LOGGING) + "/block" + std::to_string(entry.id());
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500384 auto blockObj =
385 std::make_unique<Block>(this->busLog, blockPath, entry.id());
386 this->blockingErrors.push_back(std::move(blockObj));
387
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500388 // Register call back if log is resolved
389 using namespace sdbusplus::bus::match::rules;
390 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entry.id());
391 auto callback = std::make_unique<sdbusplus::bus::match::match>(
392 this->busLog,
393 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
394 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
395 std::placeholders::_1));
396
397 propChangedEntryCallback.insert(
398 std::make_pair(entry.id(), std::move(callback)));
399
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500400 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500401}
402
Matt Spinlerc64b7122020-03-26 10:55:01 -0500403void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500404{
405 // Make the association <endpointpath>/<endpointtype> paths
406 std::vector<std::string> assocs;
407 for (const auto& [forwardType, reverseType, endpoint] :
408 entry.associations())
409 {
410 std::string e{endpoint};
411 e += '/' + reverseType;
412 assocs.push_back(e);
413 }
414
415 for (auto& create : Extensions::getCreateFunctions())
416 {
417 try
418 {
419 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500420 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500421 }
422 catch (std::exception& e)
423 {
424 log<level::ERR>("An extension's create function threw an exception",
425 phosphor::logging::entry("ERROR=%s", e.what()));
426 }
427 }
428}
429
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600430void Manager::processMetadata(const std::string& errorName,
431 const std::vector<std::string>& additionalData,
432 AssociationList& objects) const
433{
434 // additionalData is a list of "metadata=value"
435 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700436 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600437 {
Patrick Venture34438962018-10-30 13:17:37 -0700438 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700439 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600440 {
Patrick Venture34438962018-10-30 13:17:37 -0700441 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600442 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700443 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600444 {
445 (iter->second)(metadata, additionalData, objects);
446 }
447 }
448 }
449}
450
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500451void Manager::checkAndRemoveBlockingError(uint32_t entryId)
452{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500453 // First look for blocking object and remove
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500454 auto it = find_if(
455 blockingErrors.begin(), blockingErrors.end(),
456 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
457 if (it != blockingErrors.end())
458 {
459 blockingErrors.erase(it);
460 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500461
462 // Now remove the callback looking for the error to be resolved
463 auto resolveFind = propChangedEntryCallback.find(entryId);
464 if (resolveFind != propChangedEntryCallback.end())
465 {
466 propChangedEntryCallback.erase(resolveFind);
467 }
468
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500469 return;
470}
471
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500472void Manager::erase(uint32_t entryId)
473{
Patrick Venture34438962018-10-30 13:17:37 -0700474 auto entryFound = entries.find(entryId);
475 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500476 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500477 for (auto& func : Extensions::getDeleteProhibitedFunctions())
478 {
479 try
480 {
481 bool prohibited = false;
482 func(entryId, prohibited);
483 if (prohibited)
484 {
485 // Future work remains to throw an error here.
486 return;
487 }
488 }
489 catch (std::exception& e)
490 {
491 log<level::ERR>(
492 "An extension's deleteProhibited function threw "
493 "an exception",
494 entry("ERROR=%s", e.what()));
495 }
496 }
497
Deepak Kodihalli33887992017-06-13 07:06:49 -0500498 // Delete the persistent representation of this error.
499 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600500 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500501 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600502
Patrick Venturef18bf832018-10-26 18:14:00 -0700503 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600504 auto it = std::find(ids.begin(), ids.end(), id);
505 if (it != ids.end())
506 {
507 ids.erase(it);
508 }
509 };
Patrick Venture34438962018-10-30 13:17:37 -0700510 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500511 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600512 removeId(infoErrors, entryId);
513 }
514 else
515 {
516 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500517 }
Patrick Venture34438962018-10-30 13:17:37 -0700518 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500519
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500520 checkAndRemoveBlockingError(entryId);
521
Matt Spinler99c2b402019-05-23 14:29:16 -0500522 for (auto& remove : Extensions::getDeleteFunctions())
523 {
524 try
525 {
526 remove(entryId);
527 }
528 catch (std::exception& e)
529 {
530 log<level::ERR>("An extension's delete function threw an "
531 "exception",
532 entry("ERROR=%s", e.what()));
533 }
534 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500535 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600536 else
537 {
538 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700539 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600540 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500541}
542
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500543void Manager::restore()
544{
Patrick Venturef18bf832018-10-26 18:14:00 -0700545 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600546 return id == restoredId;
547 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500548 std::vector<uint32_t> errorIds;
549
550 fs::path dir(ERRLOG_PERSIST_PATH);
551 if (!fs::exists(dir) || fs::is_empty(dir))
552 {
553 return;
554 }
555
Patrick Venturef18bf832018-10-26 18:14:00 -0700556 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500557 {
558 auto id = file.path().filename().c_str();
559 auto idNum = std::stol(id);
560 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700561 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500562 if (deserialize(file.path(), *e))
563 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700564 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600565 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500566 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600567 e->emit_object_added();
568 if (e->severity() >= Entry::sevLowerLimit)
569 {
570 infoErrors.push_back(idNum);
571 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600572 else
573 {
574 realErrors.push_back(idNum);
575 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600576
577 entries.insert(std::make_pair(idNum, std::move(e)));
578 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500579 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600580 else
581 {
582 logging::log<logging::level::ERR>(
583 "Failed in sanity check while restoring error entry. "
584 "Ignoring error entry",
585 logging::entry("ID_NUM=%d", idNum),
586 logging::entry("ENTRY_ID=%d", e->id()));
587 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500588 }
589 }
590
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530591 if (!errorIds.empty())
592 {
593 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
594 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500595}
596
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500597void Manager::journalSync()
598{
599 bool syncRequested = false;
600 auto fd = -1;
601 auto rc = -1;
602 auto wd = -1;
603 auto bus = sdbusplus::bus::new_default();
604
605 auto start =
606 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
607 .count();
608
Matthew Barth59913892019-08-27 15:43:48 -0500609 // Each time an error log is committed, a request to sync the journal
610 // must occur and block that error log commit until it completes. A 5sec
611 // block is done to allow sufficient time for the journal to be synced.
612 //
613 // Number of loop iterations = 3 for the following reasons:
614 // Iteration #1: Requests a journal sync by killing the journald service.
615 // Iteration #2: Setup an inotify watch to monitor the synced file that
616 // journald updates with the timestamp the last time the
617 // journal was flushed.
618 // Iteration #3: Poll to wait until inotify reports an event which blocks
619 // the error log from being commited until the sync completes.
620 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500621 for (int i = 0; i < maxRetry; i++)
622 {
623 // Read timestamp from synced file
624 constexpr auto syncedPath = "/run/systemd/journal/synced";
625 std::ifstream syncedFile(syncedPath);
626 if (syncedFile.fail())
627 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500628 // If the synced file doesn't exist, a sync request will create it.
629 if (errno != ENOENT)
630 {
631 log<level::ERR>("Failed to open journal synced file",
632 entry("FILENAME=%s", syncedPath),
633 entry("ERRNO=%d", errno));
634 return;
635 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500636 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500637 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500638 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500639 // Only read the synced file if it exists.
640 // See if a sync happened by now
641 std::string timestampStr;
642 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700643 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500644 if (timestamp >= start)
645 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800646 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500647 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500648 }
649
650 // Let's ask for a sync, but only once
651 if (!syncRequested)
652 {
653 syncRequested = true;
654
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500655 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
656 auto signal = SIGRTMIN + 1;
657
658 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
659 SYSTEMD_INTERFACE, "KillUnit");
660 method.append(JOURNAL_UNIT, "main", signal);
661 bus.call(method);
662 if (method.is_method_error())
663 {
664 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800665 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500666 }
667 continue;
668 }
669
670 // Let's install the inotify watch, if we didn't do that yet. This watch
671 // monitors the syncedFile for when journald updates it with a newer
672 // timestamp. This means the journal has been flushed.
673 if (fd < 0)
674 {
675 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
676 if (fd < 0)
677 {
678 log<level::ERR>("Failed to create inotify watch",
679 entry("ERRNO=%d", errno));
680 return;
681 }
682
683 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
684 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
685 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
686 if (wd < 0)
687 {
688 log<level::ERR>("Failed to watch journal directory",
689 entry("PATH=%s", JOURNAL_RUN_PATH),
690 entry("ERRNO=%d", errno));
691 close(fd);
692 return;
693 }
694 continue;
695 }
696
697 // Let's wait until inotify reports an event
698 struct pollfd fds = {
699 .fd = fd,
700 .events = POLLIN,
701 };
702 constexpr auto pollTimeout = 5; // 5 seconds
703 rc = poll(&fds, 1, pollTimeout * 1000);
704 if (rc < 0)
705 {
706 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
707 entry("ERR=%s", strerror(-rc)));
708 inotify_rm_watch(fd, wd);
709 close(fd);
710 return;
711 }
712 else if (rc == 0)
713 {
714 log<level::INFO>("Poll timeout, no new journal synced data",
715 entry("TIMEOUT=%d", pollTimeout));
716 break;
717 }
718
719 // Read from the specified file descriptor until there is no new data,
720 // throwing away everything read since the timestamp will be read at the
721 // beginning of the loop.
722 constexpr auto maxBytes = 64;
723 uint8_t buffer[maxBytes];
724 while (read(fd, buffer, maxBytes) > 0)
725 ;
726 }
727
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500728 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500729 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500730 if (wd != -1)
731 {
732 inotify_rm_watch(fd, wd);
733 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500734 close(fd);
735 }
736
737 return;
738}
739
Matt Spinler1275bd12018-05-01 15:13:53 -0500740std::string Manager::readFWVersion()
741{
742 std::string version;
743 std::ifstream versionFile{BMC_VERSION_FILE};
744 std::string line;
745 static constexpr auto VERSION_ID = "VERSION_ID=";
746
747 while (std::getline(versionFile, line))
748 {
749 if (line.find(VERSION_ID) != std::string::npos)
750 {
751 auto pos = line.find_first_of('"') + 1;
752 version = line.substr(pos, line.find_last_of('"') - pos);
753 break;
754 }
755 }
756
757 if (version.empty())
758 {
759 log<level::ERR>("Unable to read BMC firmware version");
760 }
761
762 return version;
763}
764
Matt Spinler3fb83b32019-07-26 11:22:44 -0500765void Manager::create(const std::string& message, Entry::Level severity,
766 const std::map<std::string, std::string>& additionalData)
767{
768 // Convert the map into a vector of "key=value" strings
769 std::vector<std::string> ad;
770 metadata::associations::combine(additionalData, ad);
771
772 createEntry(message, severity, ad);
773}
774
Matt Spinlerc64b7122020-03-26 10:55:01 -0500775void Manager::createWithFFDC(
776 const std::string& message, Entry::Level severity,
777 const std::map<std::string, std::string>& additionalData,
778 const FFDCEntries& ffdc)
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, ffdc);
785}
786
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500787} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600788} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700789} // namespace phosphor