blob: 929ff5db8f03ab4d8bb9e383a90e6c2b8c692dec [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 Geisslerc0c500e2020-03-26 11:08:56 -0500284void Manager::checkQuiesceOnError(const Entry& entry)
285{
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500286
287 if (!isCalloutPresent(entry))
288 {
289 return;
290 }
291
292 logging::log<logging::level::INFO>(
293 "QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500294
295 std::string blockPath(OBJ_LOGGING);
296 blockPath += "/block";
297 blockPath += std::to_string(entry.id());
298 auto blockObj =
299 std::make_unique<Block>(this->busLog, blockPath, entry.id());
300 this->blockingErrors.push_back(std::move(blockObj));
301
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500302 // TODO in later commit in this series
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500303 // Call systemd to quiesce host
304 // Ensure blockingErrors removes entries when log resolved
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500305}
306
Matt Spinlerc64b7122020-03-26 10:55:01 -0500307void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500308{
309 // Make the association <endpointpath>/<endpointtype> paths
310 std::vector<std::string> assocs;
311 for (const auto& [forwardType, reverseType, endpoint] :
312 entry.associations())
313 {
314 std::string e{endpoint};
315 e += '/' + reverseType;
316 assocs.push_back(e);
317 }
318
319 for (auto& create : Extensions::getCreateFunctions())
320 {
321 try
322 {
323 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500324 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500325 }
326 catch (std::exception& e)
327 {
328 log<level::ERR>("An extension's create function threw an exception",
329 phosphor::logging::entry("ERROR=%s", e.what()));
330 }
331 }
332}
333
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600334void Manager::processMetadata(const std::string& errorName,
335 const std::vector<std::string>& additionalData,
336 AssociationList& objects) const
337{
338 // additionalData is a list of "metadata=value"
339 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700340 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600341 {
Patrick Venture34438962018-10-30 13:17:37 -0700342 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700343 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600344 {
Patrick Venture34438962018-10-30 13:17:37 -0700345 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600346 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700347 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600348 {
349 (iter->second)(metadata, additionalData, objects);
350 }
351 }
352 }
353}
354
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500355void Manager::checkAndRemoveBlockingError(uint32_t entryId)
356{
357 auto it = find_if(
358 blockingErrors.begin(), blockingErrors.end(),
359 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
360 if (it != blockingErrors.end())
361 {
362 blockingErrors.erase(it);
363 }
364 return;
365}
366
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500367void Manager::erase(uint32_t entryId)
368{
Patrick Venture34438962018-10-30 13:17:37 -0700369 auto entryFound = entries.find(entryId);
370 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500371 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500372 for (auto& func : Extensions::getDeleteProhibitedFunctions())
373 {
374 try
375 {
376 bool prohibited = false;
377 func(entryId, prohibited);
378 if (prohibited)
379 {
380 // Future work remains to throw an error here.
381 return;
382 }
383 }
384 catch (std::exception& e)
385 {
386 log<level::ERR>(
387 "An extension's deleteProhibited function threw "
388 "an exception",
389 entry("ERROR=%s", e.what()));
390 }
391 }
392
Deepak Kodihalli33887992017-06-13 07:06:49 -0500393 // Delete the persistent representation of this error.
394 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600395 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500396 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600397
Patrick Venturef18bf832018-10-26 18:14:00 -0700398 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600399 auto it = std::find(ids.begin(), ids.end(), id);
400 if (it != ids.end())
401 {
402 ids.erase(it);
403 }
404 };
Patrick Venture34438962018-10-30 13:17:37 -0700405 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500406 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600407 removeId(infoErrors, entryId);
408 }
409 else
410 {
411 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500412 }
Patrick Venture34438962018-10-30 13:17:37 -0700413 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500414
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500415 checkAndRemoveBlockingError(entryId);
416
Matt Spinler99c2b402019-05-23 14:29:16 -0500417 for (auto& remove : Extensions::getDeleteFunctions())
418 {
419 try
420 {
421 remove(entryId);
422 }
423 catch (std::exception& e)
424 {
425 log<level::ERR>("An extension's delete function threw an "
426 "exception",
427 entry("ERROR=%s", e.what()));
428 }
429 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500430 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600431 else
432 {
433 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700434 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600435 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500436}
437
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500438void Manager::restore()
439{
Patrick Venturef18bf832018-10-26 18:14:00 -0700440 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600441 return id == restoredId;
442 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500443 std::vector<uint32_t> errorIds;
444
445 fs::path dir(ERRLOG_PERSIST_PATH);
446 if (!fs::exists(dir) || fs::is_empty(dir))
447 {
448 return;
449 }
450
Patrick Venturef18bf832018-10-26 18:14:00 -0700451 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500452 {
453 auto id = file.path().filename().c_str();
454 auto idNum = std::stol(id);
455 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700456 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500457 if (deserialize(file.path(), *e))
458 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700459 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600460 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500461 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600462 e->emit_object_added();
463 if (e->severity() >= Entry::sevLowerLimit)
464 {
465 infoErrors.push_back(idNum);
466 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600467 else
468 {
469 realErrors.push_back(idNum);
470 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600471
472 entries.insert(std::make_pair(idNum, std::move(e)));
473 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500474 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600475 else
476 {
477 logging::log<logging::level::ERR>(
478 "Failed in sanity check while restoring error entry. "
479 "Ignoring error entry",
480 logging::entry("ID_NUM=%d", idNum),
481 logging::entry("ENTRY_ID=%d", e->id()));
482 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500483 }
484 }
485
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530486 if (!errorIds.empty())
487 {
488 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
489 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500490}
491
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500492void Manager::journalSync()
493{
494 bool syncRequested = false;
495 auto fd = -1;
496 auto rc = -1;
497 auto wd = -1;
498 auto bus = sdbusplus::bus::new_default();
499
500 auto start =
501 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
502 .count();
503
Matthew Barth59913892019-08-27 15:43:48 -0500504 // Each time an error log is committed, a request to sync the journal
505 // must occur and block that error log commit until it completes. A 5sec
506 // block is done to allow sufficient time for the journal to be synced.
507 //
508 // Number of loop iterations = 3 for the following reasons:
509 // Iteration #1: Requests a journal sync by killing the journald service.
510 // Iteration #2: Setup an inotify watch to monitor the synced file that
511 // journald updates with the timestamp the last time the
512 // journal was flushed.
513 // Iteration #3: Poll to wait until inotify reports an event which blocks
514 // the error log from being commited until the sync completes.
515 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500516 for (int i = 0; i < maxRetry; i++)
517 {
518 // Read timestamp from synced file
519 constexpr auto syncedPath = "/run/systemd/journal/synced";
520 std::ifstream syncedFile(syncedPath);
521 if (syncedFile.fail())
522 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500523 // If the synced file doesn't exist, a sync request will create it.
524 if (errno != ENOENT)
525 {
526 log<level::ERR>("Failed to open journal synced file",
527 entry("FILENAME=%s", syncedPath),
528 entry("ERRNO=%d", errno));
529 return;
530 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500531 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500532 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500533 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500534 // Only read the synced file if it exists.
535 // See if a sync happened by now
536 std::string timestampStr;
537 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700538 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500539 if (timestamp >= start)
540 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800541 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500542 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500543 }
544
545 // Let's ask for a sync, but only once
546 if (!syncRequested)
547 {
548 syncRequested = true;
549
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500550 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
551 auto signal = SIGRTMIN + 1;
552
553 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
554 SYSTEMD_INTERFACE, "KillUnit");
555 method.append(JOURNAL_UNIT, "main", signal);
556 bus.call(method);
557 if (method.is_method_error())
558 {
559 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800560 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500561 }
562 continue;
563 }
564
565 // Let's install the inotify watch, if we didn't do that yet. This watch
566 // monitors the syncedFile for when journald updates it with a newer
567 // timestamp. This means the journal has been flushed.
568 if (fd < 0)
569 {
570 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
571 if (fd < 0)
572 {
573 log<level::ERR>("Failed to create inotify watch",
574 entry("ERRNO=%d", errno));
575 return;
576 }
577
578 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
579 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
580 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
581 if (wd < 0)
582 {
583 log<level::ERR>("Failed to watch journal directory",
584 entry("PATH=%s", JOURNAL_RUN_PATH),
585 entry("ERRNO=%d", errno));
586 close(fd);
587 return;
588 }
589 continue;
590 }
591
592 // Let's wait until inotify reports an event
593 struct pollfd fds = {
594 .fd = fd,
595 .events = POLLIN,
596 };
597 constexpr auto pollTimeout = 5; // 5 seconds
598 rc = poll(&fds, 1, pollTimeout * 1000);
599 if (rc < 0)
600 {
601 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
602 entry("ERR=%s", strerror(-rc)));
603 inotify_rm_watch(fd, wd);
604 close(fd);
605 return;
606 }
607 else if (rc == 0)
608 {
609 log<level::INFO>("Poll timeout, no new journal synced data",
610 entry("TIMEOUT=%d", pollTimeout));
611 break;
612 }
613
614 // Read from the specified file descriptor until there is no new data,
615 // throwing away everything read since the timestamp will be read at the
616 // beginning of the loop.
617 constexpr auto maxBytes = 64;
618 uint8_t buffer[maxBytes];
619 while (read(fd, buffer, maxBytes) > 0)
620 ;
621 }
622
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500623 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500624 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500625 if (wd != -1)
626 {
627 inotify_rm_watch(fd, wd);
628 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500629 close(fd);
630 }
631
632 return;
633}
634
Matt Spinler1275bd12018-05-01 15:13:53 -0500635std::string Manager::readFWVersion()
636{
637 std::string version;
638 std::ifstream versionFile{BMC_VERSION_FILE};
639 std::string line;
640 static constexpr auto VERSION_ID = "VERSION_ID=";
641
642 while (std::getline(versionFile, line))
643 {
644 if (line.find(VERSION_ID) != std::string::npos)
645 {
646 auto pos = line.find_first_of('"') + 1;
647 version = line.substr(pos, line.find_last_of('"') - pos);
648 break;
649 }
650 }
651
652 if (version.empty())
653 {
654 log<level::ERR>("Unable to read BMC firmware version");
655 }
656
657 return version;
658}
659
Matt Spinler3fb83b32019-07-26 11:22:44 -0500660void Manager::create(const std::string& message, Entry::Level severity,
661 const std::map<std::string, std::string>& additionalData)
662{
663 // Convert the map into a vector of "key=value" strings
664 std::vector<std::string> ad;
665 metadata::associations::combine(additionalData, ad);
666
667 createEntry(message, severity, ad);
668}
669
Matt Spinlerc64b7122020-03-26 10:55:01 -0500670void Manager::createWithFFDC(
671 const std::string& message, Entry::Level severity,
672 const std::map<std::string, std::string>& additionalData,
673 const FFDCEntries& ffdc)
674{
675 // Convert the map into a vector of "key=value" strings
676 std::vector<std::string> ad;
677 metadata::associations::combine(additionalData, ad);
678
679 createEntry(message, severity, ad, ffdc);
680}
681
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500682} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600683} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700684} // namespace phosphor