blob: d636a67f982f7ff2ffe74e09e7140c104f6f4c87 [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>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060030
31using namespace phosphor::logging;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050032using namespace std::chrono;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -050033using sdbusplus::exception::SdBusError;
Deepak Kodihallia87c1572017-02-28 07:40:34 -060034extern const std::map<metadata::Metadata,
Patrick Venturef18bf832018-10-26 18:14:00 -070035 std::function<metadata::associations::Type>>
36 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050037
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060038namespace phosphor
39{
40namespace logging
41{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050042namespace internal
43{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050044inline auto getLevel(const std::string& errMsg)
45{
46 auto reqLevel = Entry::Level::Error; // Default to Error
47
48 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050049 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050050 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050051 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050052 }
53
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050054 return reqLevel;
55}
56
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050057int Manager::getRealErrSize()
58{
59 return realErrors.size();
60}
61
62int Manager::getInfoErrSize()
63{
64 return infoErrors.size();
65}
66
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050067void Manager::commit(uint64_t transactionId, std::string errMsg)
68{
69 auto level = getLevel(errMsg);
70 _commit(transactionId, std::move(errMsg), level);
71}
72
73void Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
74 uint32_t errLvl)
75{
76 _commit(transactionId, std::move(errMsg),
77 static_cast<Entry::Level>(errLvl));
78}
79
80void Manager::_commit(uint64_t transactionId, std::string&& errMsg,
81 Entry::Level errLvl)
82{
Adriana Kobylak7298dc22017-01-24 12:21:50 -060083 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -060084 // Length of 'TRANSACTION_ID' string.
Patrick Venture30047bf2018-11-01 18:52:15 -070085 constexpr const auto transactionIdVarSize = std::strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -060086 // Length of 'TRANSACTION_ID=' string.
87 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050088
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050089 // Flush all the pending log messages into the journal
90 journalSync();
Adriana Kobylakcfd9a7d2017-06-07 11:57:31 -050091
Patrick Venturef18bf832018-10-26 18:14:00 -070092 sd_journal* j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060093 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050094 if (rc < 0)
95 {
Patrick Venturef18bf832018-10-26 18:14:00 -070096 logging::log<logging::level::ERR>(
97 "Failed to open journal",
98 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060099 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500100 }
101
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600102 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -0600103 std::set<std::string> metalist;
104 auto metamap = g_errMetaMap.find(errMsg);
105 if (metamap != g_errMetaMap.end())
106 {
107 metalist.insert(metamap->second.begin(), metamap->second.end());
108 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600109
Patrick Venturef18bf832018-10-26 18:14:00 -0700110 // Add _PID field information in AdditionalData.
Jayanth Othayothdb18ebe2017-09-04 00:48:02 -0500111 metalist.insert("_PID");
112
Tom Joseph7a33ee42017-07-25 00:04:20 +0530113 std::vector<std::string> additionalData;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600114
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600115 // Read the journal from the end to get the most recent entry first.
116 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
117 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600118 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700119 const char* data = nullptr;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600120 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600121
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600122 // Look for the transaction id metadata variable
Patrick Venturef18bf832018-10-26 18:14:00 -0700123 rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data,
124 &length);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600125 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600126 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600127 // This journal entry does not have the TRANSACTION_ID
128 // metadata variable.
129 continue;
130 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600131
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600132 // journald does not guarantee that sd_journal_get_data() returns NULL
133 // terminated strings, so need to specify the size to use to compare,
134 // use the returned length instead of anything that relies on NULL
135 // terminators like strlen().
136 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
137 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
138 // 'data + transactionIdVarOffset' will be in the form of '1234'.
139 // 'length - transactionIdVarOffset' will be the length of '1234'.
140 if ((length <= (transactionIdVarOffset)) ||
Patrick Venturef18bf832018-10-26 18:14:00 -0700141 (transactionIdStr.compare(0, transactionIdStr.size(),
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600142 data + transactionIdVarOffset,
143 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600144 {
145 // The value of the TRANSACTION_ID metadata is not the requested
146 // transaction id number.
147 continue;
148 }
149
150 // Search for all metadata variables in the current journal entry.
151 for (auto i = metalist.cbegin(); i != metalist.cend();)
152 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700153 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
154 &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600155 if (rc < 0)
156 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600157 // Metadata variable not found, check next metadata variable.
158 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600159 continue;
160 }
161
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600162 // Metadata variable found, save it and remove it from the set.
163 additionalData.emplace_back(data, length);
164 i = metalist.erase(i);
165 }
166 if (metalist.empty())
167 {
168 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600169 break;
170 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600171 }
172 if (!metalist.empty())
173 {
174 // Not all the metadata variables were found in the journal.
175 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600176 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700177 logging::log<logging::level::INFO>(
178 "Failed to find metadata",
179 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600180 }
181 }
182
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500183 sd_journal_close(j);
184
Matt Spinlerb60e7552019-07-24 15:28:08 -0500185 createEntry(errMsg, errLvl, additionalData);
186}
187
188void Manager::createEntry(std::string errMsg, Entry::Level errLvl,
Matt Spinlerc64b7122020-03-26 10:55:01 -0500189 std::vector<std::string> additionalData,
190 const FFDCEntries& ffdc)
Matt Spinlerb60e7552019-07-24 15:28:08 -0500191{
192 if (!Extensions::disableDefaultLogCaps())
193 {
194 if (errLvl < Entry::sevLowerLimit)
195 {
196 if (realErrors.size() >= ERROR_CAP)
197 {
198 erase(realErrors.front());
199 }
200 }
201 else
202 {
203 if (infoErrors.size() >= ERROR_INFO_CAP)
204 {
205 erase(infoErrors.front());
206 }
207 }
208 }
209
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600210 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500211 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500212 {
213 infoErrors.push_back(entryId);
214 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600215 else
216 {
217 realErrors.push_back(entryId);
218 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600219 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700220 std::chrono::system_clock::now().time_since_epoch())
221 .count();
222 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600223
Patrick Venturef18bf832018-10-26 18:14:00 -0700224 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600225 processMetadata(errMsg, additionalData, objects);
226
Patrick Venturef18bf832018-10-26 18:14:00 -0700227 auto e = std::make_unique<Entry>(busLog, objPath, entryId,
228 ms, // Milliseconds since 1970
229 errLvl, std::move(errMsg),
230 std::move(additionalData),
231 std::move(objects), fwVersion, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500232 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500233
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500234 if (isQuiesceOnErrorEnabled())
235 {
236 checkQuiesceOnError(*e);
237 }
238
Matt Spinlerc64b7122020-03-26 10:55:01 -0500239 doExtensionLogCreate(*e, ffdc);
240
241 // Note: No need to close the file descriptors in the FFDC.
Matt Spinler99c2b402019-05-23 14:29:16 -0500242
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500243 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500244}
245
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500246bool Manager::isQuiesceOnErrorEnabled()
247{
248 std::variant<bool> property;
249
250 auto method = this->busLog.new_method_call(
251 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
252 "org.freedesktop.DBus.Properties", "Get");
253
254 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
255
256 try
257 {
258 auto reply = this->busLog.call(method);
259 reply.read(property);
260 }
261 catch (const SdBusError& e)
262 {
263 log<level::ERR>("Error reading QuiesceOnHwError property",
264 entry("ERROR=%s", e.what()));
265 throw;
266 }
267
268 return std::get<bool>(property);
269}
270
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500271bool Manager::isCalloutPresent(const Entry& entry)
272{
273 for (const auto& c : entry.additionalData())
274 {
275 if (c.find("CALLOUT_") != std::string::npos)
276 {
277 return true;
278 }
279 }
280
281 return false;
282}
283
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500284void Manager::findAndRemoveResolvedBlocks()
285{
286 for (auto& entry : entries)
287 {
288 if (entry.second->resolved())
289 {
290 checkAndRemoveBlockingError(entry.first);
291 }
292 }
293}
294
295void Manager::onEntryResolve(sdbusplus::message::message& msg)
296{
297 using Interface = std::string;
298 using Property = std::string;
299 using Value = std::string;
300 using Properties = std::map<Property, sdbusplus::message::variant<Value>>;
301
302 Interface interface;
303 Properties properties;
304
305 msg.read(interface, properties);
306
307 for (const auto& p : properties)
308 {
309 if (p.first == "Resolved")
310 {
311 findAndRemoveResolvedBlocks();
312 return;
313 }
314 }
315}
316
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500317void Manager::checkQuiesceOnError(const Entry& entry)
318{
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500319
320 if (!isCalloutPresent(entry))
321 {
322 return;
323 }
324
325 logging::log<logging::level::INFO>(
326 "QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500327
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500328 auto blockPath =
329 std::string(OBJ_LOGGING) + "/block" + std::to_string(entry.id());
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500330 auto blockObj =
331 std::make_unique<Block>(this->busLog, blockPath, entry.id());
332 this->blockingErrors.push_back(std::move(blockObj));
333
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500334 // Register call back if log is resolved
335 using namespace sdbusplus::bus::match::rules;
336 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entry.id());
337 auto callback = std::make_unique<sdbusplus::bus::match::match>(
338 this->busLog,
339 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
340 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
341 std::placeholders::_1));
342
343 propChangedEntryCallback.insert(
344 std::make_pair(entry.id(), std::move(callback)));
345
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500346 // TODO in later commit in this series
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500347 // Call systemd to quiesce host
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500348}
349
Matt Spinlerc64b7122020-03-26 10:55:01 -0500350void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500351{
352 // Make the association <endpointpath>/<endpointtype> paths
353 std::vector<std::string> assocs;
354 for (const auto& [forwardType, reverseType, endpoint] :
355 entry.associations())
356 {
357 std::string e{endpoint};
358 e += '/' + reverseType;
359 assocs.push_back(e);
360 }
361
362 for (auto& create : Extensions::getCreateFunctions())
363 {
364 try
365 {
366 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500367 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500368 }
369 catch (std::exception& e)
370 {
371 log<level::ERR>("An extension's create function threw an exception",
372 phosphor::logging::entry("ERROR=%s", e.what()));
373 }
374 }
375}
376
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600377void Manager::processMetadata(const std::string& errorName,
378 const std::vector<std::string>& additionalData,
379 AssociationList& objects) const
380{
381 // additionalData is a list of "metadata=value"
382 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700383 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600384 {
Patrick Venture34438962018-10-30 13:17:37 -0700385 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700386 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600387 {
Patrick Venture34438962018-10-30 13:17:37 -0700388 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600389 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700390 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600391 {
392 (iter->second)(metadata, additionalData, objects);
393 }
394 }
395 }
396}
397
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500398void Manager::checkAndRemoveBlockingError(uint32_t entryId)
399{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500400 // First look for blocking object and remove
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500401 auto it = find_if(
402 blockingErrors.begin(), blockingErrors.end(),
403 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
404 if (it != blockingErrors.end())
405 {
406 blockingErrors.erase(it);
407 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500408
409 // Now remove the callback looking for the error to be resolved
410 auto resolveFind = propChangedEntryCallback.find(entryId);
411 if (resolveFind != propChangedEntryCallback.end())
412 {
413 propChangedEntryCallback.erase(resolveFind);
414 }
415
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500416 return;
417}
418
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500419void Manager::erase(uint32_t entryId)
420{
Patrick Venture34438962018-10-30 13:17:37 -0700421 auto entryFound = entries.find(entryId);
422 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500423 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500424 for (auto& func : Extensions::getDeleteProhibitedFunctions())
425 {
426 try
427 {
428 bool prohibited = false;
429 func(entryId, prohibited);
430 if (prohibited)
431 {
432 // Future work remains to throw an error here.
433 return;
434 }
435 }
436 catch (std::exception& e)
437 {
438 log<level::ERR>(
439 "An extension's deleteProhibited function threw "
440 "an exception",
441 entry("ERROR=%s", e.what()));
442 }
443 }
444
Deepak Kodihalli33887992017-06-13 07:06:49 -0500445 // Delete the persistent representation of this error.
446 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600447 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500448 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600449
Patrick Venturef18bf832018-10-26 18:14:00 -0700450 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600451 auto it = std::find(ids.begin(), ids.end(), id);
452 if (it != ids.end())
453 {
454 ids.erase(it);
455 }
456 };
Patrick Venture34438962018-10-30 13:17:37 -0700457 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500458 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600459 removeId(infoErrors, entryId);
460 }
461 else
462 {
463 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500464 }
Patrick Venture34438962018-10-30 13:17:37 -0700465 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500466
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500467 checkAndRemoveBlockingError(entryId);
468
Matt Spinler99c2b402019-05-23 14:29:16 -0500469 for (auto& remove : Extensions::getDeleteFunctions())
470 {
471 try
472 {
473 remove(entryId);
474 }
475 catch (std::exception& e)
476 {
477 log<level::ERR>("An extension's delete function threw an "
478 "exception",
479 entry("ERROR=%s", e.what()));
480 }
481 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500482 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600483 else
484 {
485 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700486 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600487 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500488}
489
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500490void Manager::restore()
491{
Patrick Venturef18bf832018-10-26 18:14:00 -0700492 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600493 return id == restoredId;
494 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500495 std::vector<uint32_t> errorIds;
496
497 fs::path dir(ERRLOG_PERSIST_PATH);
498 if (!fs::exists(dir) || fs::is_empty(dir))
499 {
500 return;
501 }
502
Patrick Venturef18bf832018-10-26 18:14:00 -0700503 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500504 {
505 auto id = file.path().filename().c_str();
506 auto idNum = std::stol(id);
507 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700508 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500509 if (deserialize(file.path(), *e))
510 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700511 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600512 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500513 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600514 e->emit_object_added();
515 if (e->severity() >= Entry::sevLowerLimit)
516 {
517 infoErrors.push_back(idNum);
518 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600519 else
520 {
521 realErrors.push_back(idNum);
522 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600523
524 entries.insert(std::make_pair(idNum, std::move(e)));
525 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500526 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600527 else
528 {
529 logging::log<logging::level::ERR>(
530 "Failed in sanity check while restoring error entry. "
531 "Ignoring error entry",
532 logging::entry("ID_NUM=%d", idNum),
533 logging::entry("ENTRY_ID=%d", e->id()));
534 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500535 }
536 }
537
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530538 if (!errorIds.empty())
539 {
540 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
541 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500542}
543
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500544void Manager::journalSync()
545{
546 bool syncRequested = false;
547 auto fd = -1;
548 auto rc = -1;
549 auto wd = -1;
550 auto bus = sdbusplus::bus::new_default();
551
552 auto start =
553 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
554 .count();
555
Matthew Barth59913892019-08-27 15:43:48 -0500556 // Each time an error log is committed, a request to sync the journal
557 // must occur and block that error log commit until it completes. A 5sec
558 // block is done to allow sufficient time for the journal to be synced.
559 //
560 // Number of loop iterations = 3 for the following reasons:
561 // Iteration #1: Requests a journal sync by killing the journald service.
562 // Iteration #2: Setup an inotify watch to monitor the synced file that
563 // journald updates with the timestamp the last time the
564 // journal was flushed.
565 // Iteration #3: Poll to wait until inotify reports an event which blocks
566 // the error log from being commited until the sync completes.
567 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500568 for (int i = 0; i < maxRetry; i++)
569 {
570 // Read timestamp from synced file
571 constexpr auto syncedPath = "/run/systemd/journal/synced";
572 std::ifstream syncedFile(syncedPath);
573 if (syncedFile.fail())
574 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500575 // If the synced file doesn't exist, a sync request will create it.
576 if (errno != ENOENT)
577 {
578 log<level::ERR>("Failed to open journal synced file",
579 entry("FILENAME=%s", syncedPath),
580 entry("ERRNO=%d", errno));
581 return;
582 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500583 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500584 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500585 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500586 // Only read the synced file if it exists.
587 // See if a sync happened by now
588 std::string timestampStr;
589 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700590 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500591 if (timestamp >= start)
592 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800593 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500594 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500595 }
596
597 // Let's ask for a sync, but only once
598 if (!syncRequested)
599 {
600 syncRequested = true;
601
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500602 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
603 auto signal = SIGRTMIN + 1;
604
605 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
606 SYSTEMD_INTERFACE, "KillUnit");
607 method.append(JOURNAL_UNIT, "main", signal);
608 bus.call(method);
609 if (method.is_method_error())
610 {
611 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800612 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500613 }
614 continue;
615 }
616
617 // Let's install the inotify watch, if we didn't do that yet. This watch
618 // monitors the syncedFile for when journald updates it with a newer
619 // timestamp. This means the journal has been flushed.
620 if (fd < 0)
621 {
622 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
623 if (fd < 0)
624 {
625 log<level::ERR>("Failed to create inotify watch",
626 entry("ERRNO=%d", errno));
627 return;
628 }
629
630 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
631 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
632 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
633 if (wd < 0)
634 {
635 log<level::ERR>("Failed to watch journal directory",
636 entry("PATH=%s", JOURNAL_RUN_PATH),
637 entry("ERRNO=%d", errno));
638 close(fd);
639 return;
640 }
641 continue;
642 }
643
644 // Let's wait until inotify reports an event
645 struct pollfd fds = {
646 .fd = fd,
647 .events = POLLIN,
648 };
649 constexpr auto pollTimeout = 5; // 5 seconds
650 rc = poll(&fds, 1, pollTimeout * 1000);
651 if (rc < 0)
652 {
653 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
654 entry("ERR=%s", strerror(-rc)));
655 inotify_rm_watch(fd, wd);
656 close(fd);
657 return;
658 }
659 else if (rc == 0)
660 {
661 log<level::INFO>("Poll timeout, no new journal synced data",
662 entry("TIMEOUT=%d", pollTimeout));
663 break;
664 }
665
666 // Read from the specified file descriptor until there is no new data,
667 // throwing away everything read since the timestamp will be read at the
668 // beginning of the loop.
669 constexpr auto maxBytes = 64;
670 uint8_t buffer[maxBytes];
671 while (read(fd, buffer, maxBytes) > 0)
672 ;
673 }
674
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500675 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500676 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500677 if (wd != -1)
678 {
679 inotify_rm_watch(fd, wd);
680 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500681 close(fd);
682 }
683
684 return;
685}
686
Matt Spinler1275bd12018-05-01 15:13:53 -0500687std::string Manager::readFWVersion()
688{
689 std::string version;
690 std::ifstream versionFile{BMC_VERSION_FILE};
691 std::string line;
692 static constexpr auto VERSION_ID = "VERSION_ID=";
693
694 while (std::getline(versionFile, line))
695 {
696 if (line.find(VERSION_ID) != std::string::npos)
697 {
698 auto pos = line.find_first_of('"') + 1;
699 version = line.substr(pos, line.find_last_of('"') - pos);
700 break;
701 }
702 }
703
704 if (version.empty())
705 {
706 log<level::ERR>("Unable to read BMC firmware version");
707 }
708
709 return version;
710}
711
Matt Spinler3fb83b32019-07-26 11:22:44 -0500712void Manager::create(const std::string& message, Entry::Level severity,
713 const std::map<std::string, std::string>& additionalData)
714{
715 // Convert the map into a vector of "key=value" strings
716 std::vector<std::string> ad;
717 metadata::associations::combine(additionalData, ad);
718
719 createEntry(message, severity, ad);
720}
721
Matt Spinlerc64b7122020-03-26 10:55:01 -0500722void Manager::createWithFFDC(
723 const std::string& message, Entry::Level severity,
724 const std::map<std::string, std::string>& additionalData,
725 const FFDCEntries& ffdc)
726{
727 // Convert the map into a vector of "key=value" strings
728 std::vector<std::string> ad;
729 metadata::associations::combine(additionalData, ad);
730
731 createEntry(message, severity, ad, ffdc);
732}
733
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500734} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600735} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700736} // namespace phosphor