blob: 72a8f3c6f522a1a728facc48e3ad2f0bb31b2368 [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
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500355void Manager::erase(uint32_t entryId)
356{
Patrick Venture34438962018-10-30 13:17:37 -0700357 auto entryFound = entries.find(entryId);
358 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500359 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500360 for (auto& func : Extensions::getDeleteProhibitedFunctions())
361 {
362 try
363 {
364 bool prohibited = false;
365 func(entryId, prohibited);
366 if (prohibited)
367 {
368 // Future work remains to throw an error here.
369 return;
370 }
371 }
372 catch (std::exception& e)
373 {
374 log<level::ERR>(
375 "An extension's deleteProhibited function threw "
376 "an exception",
377 entry("ERROR=%s", e.what()));
378 }
379 }
380
Deepak Kodihalli33887992017-06-13 07:06:49 -0500381 // Delete the persistent representation of this error.
382 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600383 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500384 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600385
Patrick Venturef18bf832018-10-26 18:14:00 -0700386 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600387 auto it = std::find(ids.begin(), ids.end(), id);
388 if (it != ids.end())
389 {
390 ids.erase(it);
391 }
392 };
Patrick Venture34438962018-10-30 13:17:37 -0700393 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500394 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600395 removeId(infoErrors, entryId);
396 }
397 else
398 {
399 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500400 }
Patrick Venture34438962018-10-30 13:17:37 -0700401 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500402
403 for (auto& remove : Extensions::getDeleteFunctions())
404 {
405 try
406 {
407 remove(entryId);
408 }
409 catch (std::exception& e)
410 {
411 log<level::ERR>("An extension's delete function threw an "
412 "exception",
413 entry("ERROR=%s", e.what()));
414 }
415 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500416 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600417 else
418 {
419 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700420 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600421 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500422}
423
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500424void Manager::restore()
425{
Patrick Venturef18bf832018-10-26 18:14:00 -0700426 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600427 return id == restoredId;
428 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500429 std::vector<uint32_t> errorIds;
430
431 fs::path dir(ERRLOG_PERSIST_PATH);
432 if (!fs::exists(dir) || fs::is_empty(dir))
433 {
434 return;
435 }
436
Patrick Venturef18bf832018-10-26 18:14:00 -0700437 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500438 {
439 auto id = file.path().filename().c_str();
440 auto idNum = std::stol(id);
441 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700442 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500443 if (deserialize(file.path(), *e))
444 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700445 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600446 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500447 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600448 e->emit_object_added();
449 if (e->severity() >= Entry::sevLowerLimit)
450 {
451 infoErrors.push_back(idNum);
452 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600453 else
454 {
455 realErrors.push_back(idNum);
456 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600457
458 entries.insert(std::make_pair(idNum, std::move(e)));
459 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500460 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600461 else
462 {
463 logging::log<logging::level::ERR>(
464 "Failed in sanity check while restoring error entry. "
465 "Ignoring error entry",
466 logging::entry("ID_NUM=%d", idNum),
467 logging::entry("ENTRY_ID=%d", e->id()));
468 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500469 }
470 }
471
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530472 if (!errorIds.empty())
473 {
474 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
475 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500476}
477
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500478void Manager::journalSync()
479{
480 bool syncRequested = false;
481 auto fd = -1;
482 auto rc = -1;
483 auto wd = -1;
484 auto bus = sdbusplus::bus::new_default();
485
486 auto start =
487 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
488 .count();
489
Matthew Barth59913892019-08-27 15:43:48 -0500490 // Each time an error log is committed, a request to sync the journal
491 // must occur and block that error log commit until it completes. A 5sec
492 // block is done to allow sufficient time for the journal to be synced.
493 //
494 // Number of loop iterations = 3 for the following reasons:
495 // Iteration #1: Requests a journal sync by killing the journald service.
496 // Iteration #2: Setup an inotify watch to monitor the synced file that
497 // journald updates with the timestamp the last time the
498 // journal was flushed.
499 // Iteration #3: Poll to wait until inotify reports an event which blocks
500 // the error log from being commited until the sync completes.
501 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500502 for (int i = 0; i < maxRetry; i++)
503 {
504 // Read timestamp from synced file
505 constexpr auto syncedPath = "/run/systemd/journal/synced";
506 std::ifstream syncedFile(syncedPath);
507 if (syncedFile.fail())
508 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500509 // If the synced file doesn't exist, a sync request will create it.
510 if (errno != ENOENT)
511 {
512 log<level::ERR>("Failed to open journal synced file",
513 entry("FILENAME=%s", syncedPath),
514 entry("ERRNO=%d", errno));
515 return;
516 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500517 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500518 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500519 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500520 // Only read the synced file if it exists.
521 // See if a sync happened by now
522 std::string timestampStr;
523 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700524 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500525 if (timestamp >= start)
526 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800527 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500528 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500529 }
530
531 // Let's ask for a sync, but only once
532 if (!syncRequested)
533 {
534 syncRequested = true;
535
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500536 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
537 auto signal = SIGRTMIN + 1;
538
539 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
540 SYSTEMD_INTERFACE, "KillUnit");
541 method.append(JOURNAL_UNIT, "main", signal);
542 bus.call(method);
543 if (method.is_method_error())
544 {
545 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800546 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500547 }
548 continue;
549 }
550
551 // Let's install the inotify watch, if we didn't do that yet. This watch
552 // monitors the syncedFile for when journald updates it with a newer
553 // timestamp. This means the journal has been flushed.
554 if (fd < 0)
555 {
556 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
557 if (fd < 0)
558 {
559 log<level::ERR>("Failed to create inotify watch",
560 entry("ERRNO=%d", errno));
561 return;
562 }
563
564 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
565 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
566 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
567 if (wd < 0)
568 {
569 log<level::ERR>("Failed to watch journal directory",
570 entry("PATH=%s", JOURNAL_RUN_PATH),
571 entry("ERRNO=%d", errno));
572 close(fd);
573 return;
574 }
575 continue;
576 }
577
578 // Let's wait until inotify reports an event
579 struct pollfd fds = {
580 .fd = fd,
581 .events = POLLIN,
582 };
583 constexpr auto pollTimeout = 5; // 5 seconds
584 rc = poll(&fds, 1, pollTimeout * 1000);
585 if (rc < 0)
586 {
587 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
588 entry("ERR=%s", strerror(-rc)));
589 inotify_rm_watch(fd, wd);
590 close(fd);
591 return;
592 }
593 else if (rc == 0)
594 {
595 log<level::INFO>("Poll timeout, no new journal synced data",
596 entry("TIMEOUT=%d", pollTimeout));
597 break;
598 }
599
600 // Read from the specified file descriptor until there is no new data,
601 // throwing away everything read since the timestamp will be read at the
602 // beginning of the loop.
603 constexpr auto maxBytes = 64;
604 uint8_t buffer[maxBytes];
605 while (read(fd, buffer, maxBytes) > 0)
606 ;
607 }
608
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500609 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500610 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500611 if (wd != -1)
612 {
613 inotify_rm_watch(fd, wd);
614 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500615 close(fd);
616 }
617
618 return;
619}
620
Matt Spinler1275bd12018-05-01 15:13:53 -0500621std::string Manager::readFWVersion()
622{
623 std::string version;
624 std::ifstream versionFile{BMC_VERSION_FILE};
625 std::string line;
626 static constexpr auto VERSION_ID = "VERSION_ID=";
627
628 while (std::getline(versionFile, line))
629 {
630 if (line.find(VERSION_ID) != std::string::npos)
631 {
632 auto pos = line.find_first_of('"') + 1;
633 version = line.substr(pos, line.find_last_of('"') - pos);
634 break;
635 }
636 }
637
638 if (version.empty())
639 {
640 log<level::ERR>("Unable to read BMC firmware version");
641 }
642
643 return version;
644}
645
Matt Spinler3fb83b32019-07-26 11:22:44 -0500646void Manager::create(const std::string& message, Entry::Level severity,
647 const std::map<std::string, std::string>& additionalData)
648{
649 // Convert the map into a vector of "key=value" strings
650 std::vector<std::string> ad;
651 metadata::associations::combine(additionalData, ad);
652
653 createEntry(message, severity, ad);
654}
655
Matt Spinlerc64b7122020-03-26 10:55:01 -0500656void Manager::createWithFFDC(
657 const std::string& message, Entry::Level severity,
658 const std::map<std::string, std::string>& additionalData,
659 const FFDCEntries& ffdc)
660{
661 // Convert the map into a vector of "key=value" strings
662 std::vector<std::string> ad;
663 metadata::associations::combine(additionalData, ad);
664
665 createEntry(message, severity, ad, ffdc);
666}
667
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500668} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600669} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700670} // namespace phosphor