blob: 9e10d75dae38b2f477a6152e71303f7ea9fbcdb9 [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"
Matt Spinlerf61f2922020-06-23 11:32:49 -05009#include "util.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -070010
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050011#include <poll.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050012#include <sys/inotify.h>
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050013#include <systemd/sd-bus.h>
Adriana Kobylakd311bc82016-10-16 09:54:40 -050014#include <systemd/sd-journal.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050015#include <unistd.h>
Patrick Venturef18bf832018-10-26 18:14:00 -070016
Matt Spinler99c2b402019-05-23 14:29:16 -050017#include <cassert>
Patrick Venturef18bf832018-10-26 18:14:00 -070018#include <chrono>
19#include <cstdio>
Patrick Venture30047bf2018-11-01 18:52:15 -070020#include <cstring>
Patrick Venturef18bf832018-10-26 18:14:00 -070021#include <fstream>
Patrick Venture30047bf2018-11-01 18:52:15 -070022#include <functional>
Patrick Venturef18bf832018-10-26 18:14:00 -070023#include <future>
24#include <iostream>
Patrick Venture30047bf2018-11-01 18:52:15 -070025#include <map>
Saqib Khan2bb15192017-02-13 13:19:55 -060026#include <phosphor-logging/log.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -070027#include <sdbusplus/vtable.hpp>
28#include <set>
29#include <string>
30#include <vector>
Andrew Geisslerf6126a72020-05-08 10:41:09 -050031#include <xyz/openbmc_project/State/Host/server.hpp>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060032
33using namespace phosphor::logging;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050034using namespace std::chrono;
Andrew Geisslerc0c500e2020-03-26 11:08:56 -050035using sdbusplus::exception::SdBusError;
Deepak Kodihallia87c1572017-02-28 07:40:34 -060036extern const std::map<metadata::Metadata,
Patrick Venturef18bf832018-10-26 18:14:00 -070037 std::function<metadata::associations::Type>>
38 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050039
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060040namespace phosphor
41{
42namespace logging
43{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050044namespace internal
45{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050046inline auto getLevel(const std::string& errMsg)
47{
48 auto reqLevel = Entry::Level::Error; // Default to Error
49
50 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050051 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050052 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050053 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050054 }
55
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050056 return reqLevel;
57}
58
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050059int Manager::getRealErrSize()
60{
61 return realErrors.size();
62}
63
64int Manager::getInfoErrSize()
65{
66 return infoErrors.size();
67}
68
Lei YUb50c7052021-01-21 16:02:26 +080069uint32_t Manager::commit(uint64_t transactionId, std::string errMsg)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050070{
71 auto level = getLevel(errMsg);
72 _commit(transactionId, std::move(errMsg), level);
Lei YUb50c7052021-01-21 16:02:26 +080073 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050074}
75
Lei YUb50c7052021-01-21 16:02:26 +080076uint32_t Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
77 uint32_t errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050078{
79 _commit(transactionId, std::move(errMsg),
80 static_cast<Entry::Level>(errLvl));
Lei YUb50c7052021-01-21 16:02:26 +080081 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050082}
83
Patrick Williamsa5171972021-04-16 20:10:01 -050084void Manager::_commit(uint64_t transactionId [[maybe_unused]],
85 std::string&& errMsg, Entry::Level errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050086{
Patrick Williamsa5171972021-04-16 20:10:01 -050087 std::vector<std::string> additionalData{};
88
89 // When running as a test-case, the system may have a LOT of journal
90 // data and we may not have permissions to do some of the journal sync
91 // operations. Just skip over them.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070092 if (!IS_UNIT_TEST)
Adriana Kobylakd311bc82016-10-16 09:54:40 -050093 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070094 constexpr const auto transactionIdVar = "TRANSACTION_ID";
95 // Length of 'TRANSACTION_ID' string.
96 constexpr const auto transactionIdVarSize =
97 std::strlen(transactionIdVar);
98 // Length of 'TRANSACTION_ID=' string.
99 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500100
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700101 // Flush all the pending log messages into the journal
102 journalSync();
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600103
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700104 sd_journal* j = nullptr;
105 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600106 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600107 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700108 logging::log<logging::level::ERR>(
109 "Failed to open journal",
110 logging::entry("DESCRIPTION=%s", strerror(-rc)));
111 return;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600112 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600113
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700114 std::string transactionIdStr = std::to_string(transactionId);
115 std::set<std::string> metalist;
116 auto metamap = g_errMetaMap.find(errMsg);
117 if (metamap != g_errMetaMap.end())
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600118 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700119 metalist.insert(metamap->second.begin(), metamap->second.end());
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600120 }
121
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700122 // Add _PID field information in AdditionalData.
123 metalist.insert("_PID");
124
125 // Read the journal from the end to get the most recent entry first.
126 // The result from the sd_journal_get_data() is of the form
127 // VARIABLE=value.
128 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600129 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700130 const char* data = nullptr;
131 size_t length = 0;
132
133 // Look for the transaction id metadata variable
134 rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data,
Patrick Venturef18bf832018-10-26 18:14:00 -0700135 &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600136 if (rc < 0)
137 {
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700138 // This journal entry does not have the TRANSACTION_ID
139 // metadata variable.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600140 continue;
141 }
142
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700143 // journald does not guarantee that sd_journal_get_data() returns
144 // NULL terminated strings, so need to specify the size to use to
145 // compare, use the returned length instead of anything that relies
146 // on NULL terminators like strlen(). The data variable is in the
147 // form of 'TRANSACTION_ID=1234'. Remove the TRANSACTION_ID
148 // characters plus the (=) sign to do the comparison. 'data +
149 // transactionIdVarOffset' will be in the form of '1234'. 'length -
150 // transactionIdVarOffset' will be the length of '1234'.
151 if ((length <= (transactionIdVarOffset)) ||
152 (transactionIdStr.compare(
153 0, transactionIdStr.size(), data + transactionIdVarOffset,
154 length - transactionIdVarOffset) != 0))
155 {
156 // The value of the TRANSACTION_ID metadata is not the requested
157 // transaction id number.
158 continue;
159 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600160
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700161 // Search for all metadata variables in the current journal entry.
162 for (auto i = metalist.cbegin(); i != metalist.cend();)
163 {
164 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
165 &length);
166 if (rc < 0)
167 {
168 // Metadata variable not found, check next metadata
169 // variable.
170 i++;
171 continue;
172 }
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500173
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700174 // Metadata variable found, save it and remove it from the set.
175 additionalData.emplace_back(data, length);
176 i = metalist.erase(i);
177 }
178 if (metalist.empty())
179 {
180 // All metadata variables found, break out of journal loop.
181 break;
182 }
183 }
184 if (!metalist.empty())
185 {
186 // Not all the metadata variables were found in the journal.
187 for (auto& metaVarStr : metalist)
188 {
189 logging::log<logging::level::INFO>(
190 "Failed to find metadata",
191 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
192 }
193 }
194
195 sd_journal_close(j);
196 }
Matt Spinlerb60e7552019-07-24 15:28:08 -0500197 createEntry(errMsg, errLvl, additionalData);
198}
199
200void Manager::createEntry(std::string errMsg, Entry::Level errLvl,
Matt Spinlerc64b7122020-03-26 10:55:01 -0500201 std::vector<std::string> additionalData,
202 const FFDCEntries& ffdc)
Matt Spinlerb60e7552019-07-24 15:28:08 -0500203{
204 if (!Extensions::disableDefaultLogCaps())
205 {
206 if (errLvl < Entry::sevLowerLimit)
207 {
208 if (realErrors.size() >= ERROR_CAP)
209 {
210 erase(realErrors.front());
211 }
212 }
213 else
214 {
215 if (infoErrors.size() >= ERROR_INFO_CAP)
216 {
217 erase(infoErrors.front());
218 }
219 }
220 }
221
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600222 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500223 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500224 {
225 infoErrors.push_back(entryId);
226 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600227 else
228 {
229 realErrors.push_back(entryId);
230 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600231 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700232 std::chrono::system_clock::now().time_since_epoch())
233 .count();
234 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600235
Patrick Venturef18bf832018-10-26 18:14:00 -0700236 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600237 processMetadata(errMsg, additionalData, objects);
238
Patrick Venturef18bf832018-10-26 18:14:00 -0700239 auto e = std::make_unique<Entry>(busLog, objPath, entryId,
240 ms, // Milliseconds since 1970
241 errLvl, std::move(errMsg),
242 std::move(additionalData),
243 std::move(objects), fwVersion, *this);
Adriana Kobylak1ff95ef2020-12-03 13:52:19 -0600244 auto path = serialize(*e);
245 e->path(path);
Matt Spinler99c2b402019-05-23 14:29:16 -0500246
Andrew Geissler32874542020-07-09 09:17:03 -0500247 if (isQuiesceOnErrorEnabled() && isCalloutPresent(*e))
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500248 {
Andrew Geissler32874542020-07-09 09:17:03 -0500249 quiesceOnError(entryId);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500250 }
251
Adriana Kobylake7d271a2020-12-07 14:32:44 -0600252 // Add entry before calling the extensions so that they have access to it
253 entries.insert(std::make_pair(entryId, std::move(e)));
254
255 doExtensionLogCreate(*entries.find(entryId)->second, ffdc);
Matt Spinlerc64b7122020-03-26 10:55:01 -0500256
257 // Note: No need to close the file descriptors in the FFDC.
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500258}
259
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500260bool Manager::isQuiesceOnErrorEnabled()
261{
Patrick Williamsa5171972021-04-16 20:10:01 -0500262 // When running under tests, the Logging.Settings service will not be
263 // present. Assume false.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700264 if (IS_UNIT_TEST)
265 {
266 return false;
267 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500268
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500269 std::variant<bool> property;
270
271 auto method = this->busLog.new_method_call(
272 "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings",
273 "org.freedesktop.DBus.Properties", "Get");
274
275 method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError");
276
277 try
278 {
279 auto reply = this->busLog.call(method);
280 reply.read(property);
281 }
282 catch (const SdBusError& e)
283 {
284 log<level::ERR>("Error reading QuiesceOnHwError property",
285 entry("ERROR=%s", e.what()));
286 throw;
287 }
288
289 return std::get<bool>(property);
290}
291
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500292bool Manager::isCalloutPresent(const Entry& entry)
293{
294 for (const auto& c : entry.additionalData())
295 {
296 if (c.find("CALLOUT_") != std::string::npos)
297 {
298 return true;
299 }
300 }
301
302 return false;
303}
304
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500305void Manager::findAndRemoveResolvedBlocks()
306{
307 for (auto& entry : entries)
308 {
309 if (entry.second->resolved())
310 {
311 checkAndRemoveBlockingError(entry.first);
312 }
313 }
314}
315
316void Manager::onEntryResolve(sdbusplus::message::message& msg)
317{
318 using Interface = std::string;
319 using Property = std::string;
320 using Value = std::string;
Patrick Williams25acc6c2020-06-02 11:05:34 -0500321 using Properties = std::map<Property, std::variant<Value>>;
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500322
323 Interface interface;
324 Properties properties;
325
326 msg.read(interface, properties);
327
328 for (const auto& p : properties)
329 {
330 if (p.first == "Resolved")
331 {
332 findAndRemoveResolvedBlocks();
333 return;
334 }
335 }
336}
337
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500338void Manager::checkAndQuiesceHost()
339{
340 // First check host state
341 std::variant<std::string> property;
342
343 auto method = this->busLog.new_method_call(
344 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
345 "org.freedesktop.DBus.Properties", "Get");
346
347 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
348
349 try
350 {
351 auto reply = this->busLog.call(method);
352 reply.read(property);
353 }
354 catch (const SdBusError& e)
355 {
356 // Quiescing the host is a "best effort" type function. If unable to
357 // read the host state or it comes back empty, just return.
358 // The boot block object will still be created and the associations to
359 // find the log will be present. Don't want a dependency with
360 // phosphor-state-manager service
361 log<level::INFO>("Error reading QuiesceOnHwError property",
362 entry("ERROR=%s", e.what()));
363 return;
364 }
365
366 std::string hostState = std::get<std::string>(property);
367
368 // If host state is empty, do nothing
369 if (hostState.empty())
370 {
371 return;
372 }
373
374 using Host = sdbusplus::xyz::openbmc_project::State::server::Host;
375 auto state = Host::convertHostStateFromString(hostState);
376 if (state != Host::HostState::Running)
377 {
378 return;
379 }
380
381 auto quiesce = this->busLog.new_method_call(
382 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
383 "org.freedesktop.systemd1.Manager", "StartUnit");
384
385 quiesce.append("obmc-host-quiesce@0.target");
386 quiesce.append("replace");
387
388 this->busLog.call_noreply(quiesce);
389}
390
Andrew Geissler32874542020-07-09 09:17:03 -0500391void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500392{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500393 // Verify we don't already have this entry blocking
394 auto it = find_if(
395 this->blockingErrors.begin(), this->blockingErrors.end(),
396 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
397 if (it != this->blockingErrors.end())
398 {
399 // Already recorded so just return
400 logging::log<logging::level::DEBUG>(
401 "QuiesceOnError set and callout present but entry already logged");
402 return;
403 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500404
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500405 logging::log<logging::level::INFO>(
406 "QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500407
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500408 auto blockPath =
Andrew Geissler32874542020-07-09 09:17:03 -0500409 std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId);
410 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500411 this->blockingErrors.push_back(std::move(blockObj));
412
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500413 // Register call back if log is resolved
414 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500415 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500416 auto callback = std::make_unique<sdbusplus::bus::match::match>(
417 this->busLog,
418 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
419 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
420 std::placeholders::_1));
421
422 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500423 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500424
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500425 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500426}
427
Matt Spinlerc64b7122020-03-26 10:55:01 -0500428void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500429{
430 // Make the association <endpointpath>/<endpointtype> paths
431 std::vector<std::string> assocs;
432 for (const auto& [forwardType, reverseType, endpoint] :
433 entry.associations())
434 {
435 std::string e{endpoint};
436 e += '/' + reverseType;
437 assocs.push_back(e);
438 }
439
440 for (auto& create : Extensions::getCreateFunctions())
441 {
442 try
443 {
444 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500445 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500446 }
447 catch (std::exception& e)
448 {
449 log<level::ERR>("An extension's create function threw an exception",
450 phosphor::logging::entry("ERROR=%s", e.what()));
451 }
452 }
453}
454
Patrick Williamsf40323d2021-04-16 15:35:17 -0500455void Manager::processMetadata(const std::string& /*errorName*/,
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600456 const std::vector<std::string>& additionalData,
457 AssociationList& objects) const
458{
459 // additionalData is a list of "metadata=value"
460 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700461 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600462 {
Patrick Venture34438962018-10-30 13:17:37 -0700463 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700464 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600465 {
Patrick Venture34438962018-10-30 13:17:37 -0700466 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600467 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700468 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600469 {
470 (iter->second)(metadata, additionalData, objects);
471 }
472 }
473 }
474}
475
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500476void Manager::checkAndRemoveBlockingError(uint32_t entryId)
477{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500478 // First look for blocking object and remove
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500479 auto it = find_if(
480 blockingErrors.begin(), blockingErrors.end(),
481 [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; });
482 if (it != blockingErrors.end())
483 {
484 blockingErrors.erase(it);
485 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500486
487 // Now remove the callback looking for the error to be resolved
488 auto resolveFind = propChangedEntryCallback.find(entryId);
489 if (resolveFind != propChangedEntryCallback.end())
490 {
491 propChangedEntryCallback.erase(resolveFind);
492 }
493
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500494 return;
495}
496
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500497void Manager::erase(uint32_t entryId)
498{
Patrick Venture34438962018-10-30 13:17:37 -0700499 auto entryFound = entries.find(entryId);
500 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500501 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500502 for (auto& func : Extensions::getDeleteProhibitedFunctions())
503 {
504 try
505 {
506 bool prohibited = false;
507 func(entryId, prohibited);
508 if (prohibited)
509 {
510 // Future work remains to throw an error here.
511 return;
512 }
513 }
514 catch (std::exception& e)
515 {
516 log<level::ERR>(
517 "An extension's deleteProhibited function threw "
518 "an exception",
519 entry("ERROR=%s", e.what()));
520 }
521 }
522
Deepak Kodihalli33887992017-06-13 07:06:49 -0500523 // Delete the persistent representation of this error.
524 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600525 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500526 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600527
Patrick Venturef18bf832018-10-26 18:14:00 -0700528 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600529 auto it = std::find(ids.begin(), ids.end(), id);
530 if (it != ids.end())
531 {
532 ids.erase(it);
533 }
534 };
Patrick Venture34438962018-10-30 13:17:37 -0700535 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500536 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600537 removeId(infoErrors, entryId);
538 }
539 else
540 {
541 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500542 }
Patrick Venture34438962018-10-30 13:17:37 -0700543 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500544
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500545 checkAndRemoveBlockingError(entryId);
546
Matt Spinler99c2b402019-05-23 14:29:16 -0500547 for (auto& remove : Extensions::getDeleteFunctions())
548 {
549 try
550 {
551 remove(entryId);
552 }
553 catch (std::exception& e)
554 {
555 log<level::ERR>("An extension's delete function threw an "
556 "exception",
557 entry("ERROR=%s", e.what()));
558 }
559 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500560 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600561 else
562 {
563 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700564 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600565 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500566}
567
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500568void Manager::restore()
569{
Patrick Venturef18bf832018-10-26 18:14:00 -0700570 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600571 return id == restoredId;
572 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500573 std::vector<uint32_t> errorIds;
574
575 fs::path dir(ERRLOG_PERSIST_PATH);
576 if (!fs::exists(dir) || fs::is_empty(dir))
577 {
578 return;
579 }
580
Patrick Venturef18bf832018-10-26 18:14:00 -0700581 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500582 {
583 auto id = file.path().filename().c_str();
584 auto idNum = std::stol(id);
585 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700586 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500587 if (deserialize(file.path(), *e))
588 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700589 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600590 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500591 {
Adriana Kobylak1ff95ef2020-12-03 13:52:19 -0600592 e->path(file.path());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600593 e->emit_object_added();
594 if (e->severity() >= Entry::sevLowerLimit)
595 {
596 infoErrors.push_back(idNum);
597 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600598 else
599 {
600 realErrors.push_back(idNum);
601 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600602
603 entries.insert(std::make_pair(idNum, std::move(e)));
604 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500605 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600606 else
607 {
608 logging::log<logging::level::ERR>(
609 "Failed in sanity check while restoring error entry. "
610 "Ignoring error entry",
611 logging::entry("ID_NUM=%d", idNum),
612 logging::entry("ENTRY_ID=%d", e->id()));
613 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500614 }
615 }
616
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530617 if (!errorIds.empty())
618 {
619 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
620 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500621}
622
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500623void Manager::journalSync()
624{
625 bool syncRequested = false;
626 auto fd = -1;
627 auto rc = -1;
628 auto wd = -1;
629 auto bus = sdbusplus::bus::new_default();
630
631 auto start =
632 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
633 .count();
634
Matthew Barth59913892019-08-27 15:43:48 -0500635 // Each time an error log is committed, a request to sync the journal
636 // must occur and block that error log commit until it completes. A 5sec
637 // block is done to allow sufficient time for the journal to be synced.
638 //
639 // Number of loop iterations = 3 for the following reasons:
640 // Iteration #1: Requests a journal sync by killing the journald service.
641 // Iteration #2: Setup an inotify watch to monitor the synced file that
642 // journald updates with the timestamp the last time the
643 // journal was flushed.
644 // Iteration #3: Poll to wait until inotify reports an event which blocks
645 // the error log from being commited until the sync completes.
646 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500647 for (int i = 0; i < maxRetry; i++)
648 {
649 // Read timestamp from synced file
650 constexpr auto syncedPath = "/run/systemd/journal/synced";
651 std::ifstream syncedFile(syncedPath);
652 if (syncedFile.fail())
653 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500654 // If the synced file doesn't exist, a sync request will create it.
655 if (errno != ENOENT)
656 {
657 log<level::ERR>("Failed to open journal synced file",
658 entry("FILENAME=%s", syncedPath),
659 entry("ERRNO=%d", errno));
660 return;
661 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500662 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500663 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500664 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500665 // Only read the synced file if it exists.
666 // See if a sync happened by now
667 std::string timestampStr;
668 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700669 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500670 if (timestamp >= start)
671 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800672 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500673 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500674 }
675
676 // Let's ask for a sync, but only once
677 if (!syncRequested)
678 {
679 syncRequested = true;
680
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500681 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
682 auto signal = SIGRTMIN + 1;
683
684 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
685 SYSTEMD_INTERFACE, "KillUnit");
686 method.append(JOURNAL_UNIT, "main", signal);
687 bus.call(method);
688 if (method.is_method_error())
689 {
690 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800691 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500692 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500693
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500694 continue;
695 }
696
697 // Let's install the inotify watch, if we didn't do that yet. This watch
698 // monitors the syncedFile for when journald updates it with a newer
699 // timestamp. This means the journal has been flushed.
700 if (fd < 0)
701 {
702 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
703 if (fd < 0)
704 {
705 log<level::ERR>("Failed to create inotify watch",
706 entry("ERRNO=%d", errno));
707 return;
708 }
709
710 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
711 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
712 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
713 if (wd < 0)
714 {
715 log<level::ERR>("Failed to watch journal directory",
716 entry("PATH=%s", JOURNAL_RUN_PATH),
717 entry("ERRNO=%d", errno));
718 close(fd);
719 return;
720 }
721 continue;
722 }
723
724 // Let's wait until inotify reports an event
725 struct pollfd fds = {
726 .fd = fd,
727 .events = POLLIN,
Patrick Williamsf40323d2021-04-16 15:35:17 -0500728 .revents = 0,
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500729 };
730 constexpr auto pollTimeout = 5; // 5 seconds
731 rc = poll(&fds, 1, pollTimeout * 1000);
732 if (rc < 0)
733 {
734 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
735 entry("ERR=%s", strerror(-rc)));
736 inotify_rm_watch(fd, wd);
737 close(fd);
738 return;
739 }
740 else if (rc == 0)
741 {
742 log<level::INFO>("Poll timeout, no new journal synced data",
743 entry("TIMEOUT=%d", pollTimeout));
744 break;
745 }
746
747 // Read from the specified file descriptor until there is no new data,
748 // throwing away everything read since the timestamp will be read at the
749 // beginning of the loop.
750 constexpr auto maxBytes = 64;
751 uint8_t buffer[maxBytes];
752 while (read(fd, buffer, maxBytes) > 0)
753 ;
754 }
755
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500756 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500757 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500758 if (wd != -1)
759 {
760 inotify_rm_watch(fd, wd);
761 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500762 close(fd);
763 }
764
765 return;
766}
767
Matt Spinler1275bd12018-05-01 15:13:53 -0500768std::string Manager::readFWVersion()
769{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500770 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500771
Matt Spinlerf61f2922020-06-23 11:32:49 -0500772 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500773 {
774 log<level::ERR>("Unable to read BMC firmware version");
775 }
776
Matt Spinlerf61f2922020-06-23 11:32:49 -0500777 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500778}
779
Matt Spinler3fb83b32019-07-26 11:22:44 -0500780void Manager::create(const std::string& message, Entry::Level severity,
781 const std::map<std::string, std::string>& additionalData)
782{
783 // Convert the map into a vector of "key=value" strings
784 std::vector<std::string> ad;
785 metadata::associations::combine(additionalData, ad);
786
787 createEntry(message, severity, ad);
788}
789
Matt Spinlerc64b7122020-03-26 10:55:01 -0500790void Manager::createWithFFDC(
791 const std::string& message, Entry::Level severity,
792 const std::map<std::string, std::string>& additionalData,
793 const FFDCEntries& ffdc)
794{
795 // Convert the map into a vector of "key=value" strings
796 std::vector<std::string> ad;
797 metadata::associations::combine(additionalData, ad);
798
799 createEntry(message, severity, ad, ffdc);
800}
801
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500802} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600803} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700804} // namespace phosphor