blob: cf2a79541dfb62f25801b5e981f4b673a3038a88 [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 Geisslerc0c500e2020-03-26 11:08:56 -0500294 // TODO in later commit in this series
295}
296
Matt Spinlerc64b7122020-03-26 10:55:01 -0500297void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500298{
299 // Make the association <endpointpath>/<endpointtype> paths
300 std::vector<std::string> assocs;
301 for (const auto& [forwardType, reverseType, endpoint] :
302 entry.associations())
303 {
304 std::string e{endpoint};
305 e += '/' + reverseType;
306 assocs.push_back(e);
307 }
308
309 for (auto& create : Extensions::getCreateFunctions())
310 {
311 try
312 {
313 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500314 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500315 }
316 catch (std::exception& e)
317 {
318 log<level::ERR>("An extension's create function threw an exception",
319 phosphor::logging::entry("ERROR=%s", e.what()));
320 }
321 }
322}
323
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600324void Manager::processMetadata(const std::string& errorName,
325 const std::vector<std::string>& additionalData,
326 AssociationList& objects) const
327{
328 // additionalData is a list of "metadata=value"
329 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700330 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600331 {
Patrick Venture34438962018-10-30 13:17:37 -0700332 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700333 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600334 {
Patrick Venture34438962018-10-30 13:17:37 -0700335 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600336 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700337 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600338 {
339 (iter->second)(metadata, additionalData, objects);
340 }
341 }
342 }
343}
344
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500345void Manager::erase(uint32_t entryId)
346{
Patrick Venture34438962018-10-30 13:17:37 -0700347 auto entryFound = entries.find(entryId);
348 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500349 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500350 for (auto& func : Extensions::getDeleteProhibitedFunctions())
351 {
352 try
353 {
354 bool prohibited = false;
355 func(entryId, prohibited);
356 if (prohibited)
357 {
358 // Future work remains to throw an error here.
359 return;
360 }
361 }
362 catch (std::exception& e)
363 {
364 log<level::ERR>(
365 "An extension's deleteProhibited function threw "
366 "an exception",
367 entry("ERROR=%s", e.what()));
368 }
369 }
370
Deepak Kodihalli33887992017-06-13 07:06:49 -0500371 // Delete the persistent representation of this error.
372 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600373 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500374 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600375
Patrick Venturef18bf832018-10-26 18:14:00 -0700376 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600377 auto it = std::find(ids.begin(), ids.end(), id);
378 if (it != ids.end())
379 {
380 ids.erase(it);
381 }
382 };
Patrick Venture34438962018-10-30 13:17:37 -0700383 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500384 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600385 removeId(infoErrors, entryId);
386 }
387 else
388 {
389 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500390 }
Patrick Venture34438962018-10-30 13:17:37 -0700391 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500392
393 for (auto& remove : Extensions::getDeleteFunctions())
394 {
395 try
396 {
397 remove(entryId);
398 }
399 catch (std::exception& e)
400 {
401 log<level::ERR>("An extension's delete function threw an "
402 "exception",
403 entry("ERROR=%s", e.what()));
404 }
405 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500406 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600407 else
408 {
409 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700410 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600411 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500412}
413
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500414void Manager::restore()
415{
Patrick Venturef18bf832018-10-26 18:14:00 -0700416 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600417 return id == restoredId;
418 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500419 std::vector<uint32_t> errorIds;
420
421 fs::path dir(ERRLOG_PERSIST_PATH);
422 if (!fs::exists(dir) || fs::is_empty(dir))
423 {
424 return;
425 }
426
Patrick Venturef18bf832018-10-26 18:14:00 -0700427 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500428 {
429 auto id = file.path().filename().c_str();
430 auto idNum = std::stol(id);
431 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700432 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500433 if (deserialize(file.path(), *e))
434 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700435 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600436 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500437 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600438 e->emit_object_added();
439 if (e->severity() >= Entry::sevLowerLimit)
440 {
441 infoErrors.push_back(idNum);
442 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600443 else
444 {
445 realErrors.push_back(idNum);
446 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600447
448 entries.insert(std::make_pair(idNum, std::move(e)));
449 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500450 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600451 else
452 {
453 logging::log<logging::level::ERR>(
454 "Failed in sanity check while restoring error entry. "
455 "Ignoring error entry",
456 logging::entry("ID_NUM=%d", idNum),
457 logging::entry("ENTRY_ID=%d", e->id()));
458 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500459 }
460 }
461
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530462 if (!errorIds.empty())
463 {
464 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
465 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500466}
467
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500468void Manager::journalSync()
469{
470 bool syncRequested = false;
471 auto fd = -1;
472 auto rc = -1;
473 auto wd = -1;
474 auto bus = sdbusplus::bus::new_default();
475
476 auto start =
477 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
478 .count();
479
Matthew Barth59913892019-08-27 15:43:48 -0500480 // Each time an error log is committed, a request to sync the journal
481 // must occur and block that error log commit until it completes. A 5sec
482 // block is done to allow sufficient time for the journal to be synced.
483 //
484 // Number of loop iterations = 3 for the following reasons:
485 // Iteration #1: Requests a journal sync by killing the journald service.
486 // Iteration #2: Setup an inotify watch to monitor the synced file that
487 // journald updates with the timestamp the last time the
488 // journal was flushed.
489 // Iteration #3: Poll to wait until inotify reports an event which blocks
490 // the error log from being commited until the sync completes.
491 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500492 for (int i = 0; i < maxRetry; i++)
493 {
494 // Read timestamp from synced file
495 constexpr auto syncedPath = "/run/systemd/journal/synced";
496 std::ifstream syncedFile(syncedPath);
497 if (syncedFile.fail())
498 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500499 // If the synced file doesn't exist, a sync request will create it.
500 if (errno != ENOENT)
501 {
502 log<level::ERR>("Failed to open journal synced file",
503 entry("FILENAME=%s", syncedPath),
504 entry("ERRNO=%d", errno));
505 return;
506 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500507 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500508 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500509 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500510 // Only read the synced file if it exists.
511 // See if a sync happened by now
512 std::string timestampStr;
513 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700514 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500515 if (timestamp >= start)
516 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800517 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500518 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500519 }
520
521 // Let's ask for a sync, but only once
522 if (!syncRequested)
523 {
524 syncRequested = true;
525
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500526 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
527 auto signal = SIGRTMIN + 1;
528
529 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
530 SYSTEMD_INTERFACE, "KillUnit");
531 method.append(JOURNAL_UNIT, "main", signal);
532 bus.call(method);
533 if (method.is_method_error())
534 {
535 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800536 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500537 }
538 continue;
539 }
540
541 // Let's install the inotify watch, if we didn't do that yet. This watch
542 // monitors the syncedFile for when journald updates it with a newer
543 // timestamp. This means the journal has been flushed.
544 if (fd < 0)
545 {
546 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
547 if (fd < 0)
548 {
549 log<level::ERR>("Failed to create inotify watch",
550 entry("ERRNO=%d", errno));
551 return;
552 }
553
554 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
555 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
556 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
557 if (wd < 0)
558 {
559 log<level::ERR>("Failed to watch journal directory",
560 entry("PATH=%s", JOURNAL_RUN_PATH),
561 entry("ERRNO=%d", errno));
562 close(fd);
563 return;
564 }
565 continue;
566 }
567
568 // Let's wait until inotify reports an event
569 struct pollfd fds = {
570 .fd = fd,
571 .events = POLLIN,
572 };
573 constexpr auto pollTimeout = 5; // 5 seconds
574 rc = poll(&fds, 1, pollTimeout * 1000);
575 if (rc < 0)
576 {
577 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
578 entry("ERR=%s", strerror(-rc)));
579 inotify_rm_watch(fd, wd);
580 close(fd);
581 return;
582 }
583 else if (rc == 0)
584 {
585 log<level::INFO>("Poll timeout, no new journal synced data",
586 entry("TIMEOUT=%d", pollTimeout));
587 break;
588 }
589
590 // Read from the specified file descriptor until there is no new data,
591 // throwing away everything read since the timestamp will be read at the
592 // beginning of the loop.
593 constexpr auto maxBytes = 64;
594 uint8_t buffer[maxBytes];
595 while (read(fd, buffer, maxBytes) > 0)
596 ;
597 }
598
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500599 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500600 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500601 if (wd != -1)
602 {
603 inotify_rm_watch(fd, wd);
604 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500605 close(fd);
606 }
607
608 return;
609}
610
Matt Spinler1275bd12018-05-01 15:13:53 -0500611std::string Manager::readFWVersion()
612{
613 std::string version;
614 std::ifstream versionFile{BMC_VERSION_FILE};
615 std::string line;
616 static constexpr auto VERSION_ID = "VERSION_ID=";
617
618 while (std::getline(versionFile, line))
619 {
620 if (line.find(VERSION_ID) != std::string::npos)
621 {
622 auto pos = line.find_first_of('"') + 1;
623 version = line.substr(pos, line.find_last_of('"') - pos);
624 break;
625 }
626 }
627
628 if (version.empty())
629 {
630 log<level::ERR>("Unable to read BMC firmware version");
631 }
632
633 return version;
634}
635
Matt Spinler3fb83b32019-07-26 11:22:44 -0500636void Manager::create(const std::string& message, Entry::Level severity,
637 const std::map<std::string, std::string>& additionalData)
638{
639 // Convert the map into a vector of "key=value" strings
640 std::vector<std::string> ad;
641 metadata::associations::combine(additionalData, ad);
642
643 createEntry(message, severity, ad);
644}
645
Matt Spinlerc64b7122020-03-26 10:55:01 -0500646void Manager::createWithFFDC(
647 const std::string& message, Entry::Level severity,
648 const std::map<std::string, std::string>& additionalData,
649 const FFDCEntries& ffdc)
650{
651 // Convert the map into a vector of "key=value" strings
652 std::vector<std::string> ad;
653 metadata::associations::combine(additionalData, ad);
654
655 createEntry(message, severity, ad, ffdc);
656}
657
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500658} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600659} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700660} // namespace phosphor