blob: f2d1ef6afcd21989a1a3f14796b46d8fca715c88 [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
Patrick Williams2544b412022-10-04 08:41:06 -050017#include <phosphor-logging/lg2.hpp>
18#include <sdbusplus/vtable.hpp>
19#include <xyz/openbmc_project/State/Host/server.hpp>
20
Matt Spinler99c2b402019-05-23 14:29:16 -050021#include <cassert>
Patrick Venturef18bf832018-10-26 18:14:00 -070022#include <chrono>
23#include <cstdio>
Patrick Venture30047bf2018-11-01 18:52:15 -070024#include <cstring>
Patrick Venturef18bf832018-10-26 18:14:00 -070025#include <fstream>
Patrick Venture30047bf2018-11-01 18:52:15 -070026#include <functional>
Patrick Venturef18bf832018-10-26 18:14:00 -070027#include <future>
28#include <iostream>
Patrick Venture30047bf2018-11-01 18:52:15 -070029#include <map>
Patrick Venturef18bf832018-10-26 18:14:00 -070030#include <set>
31#include <string>
Patrick Williamsb01a5b42021-08-28 15:11:45 -050032#include <string_view>
Patrick Venturef18bf832018-10-26 18:14:00 -070033#include <vector>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060034
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050035using namespace std::chrono;
Patrick Williams5f285c52021-07-27 21:25:38 -050036extern const std::map<
37 phosphor::logging::metadata::Metadata,
38 std::function<phosphor::logging::metadata::associations::Type>>
Patrick Venturef18bf832018-10-26 18:14:00 -070039 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050040
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060041namespace phosphor
42{
43namespace logging
44{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050045namespace internal
46{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050047inline auto getLevel(const std::string& errMsg)
48{
49 auto reqLevel = Entry::Level::Error; // Default to Error
50
51 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050052 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050053 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050054 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050055 }
56
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050057 return reqLevel;
58}
59
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050060int Manager::getRealErrSize()
61{
62 return realErrors.size();
63}
64
65int Manager::getInfoErrSize()
66{
67 return infoErrors.size();
68}
69
Lei YUb50c7052021-01-21 16:02:26 +080070uint32_t Manager::commit(uint64_t transactionId, std::string errMsg)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050071{
72 auto level = getLevel(errMsg);
73 _commit(transactionId, std::move(errMsg), level);
Lei YUb50c7052021-01-21 16:02:26 +080074 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050075}
76
Lei YUb50c7052021-01-21 16:02:26 +080077uint32_t Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
78 uint32_t errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050079{
80 _commit(transactionId, std::move(errMsg),
81 static_cast<Entry::Level>(errLvl));
Lei YUb50c7052021-01-21 16:02:26 +080082 return entryId;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050083}
84
Patrick Williamsa5171972021-04-16 20:10:01 -050085void Manager::_commit(uint64_t transactionId [[maybe_unused]],
86 std::string&& errMsg, Entry::Level errLvl)
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050087{
Patrick Williamsa5171972021-04-16 20:10:01 -050088 std::vector<std::string> additionalData{};
89
90 // When running as a test-case, the system may have a LOT of journal
91 // data and we may not have permissions to do some of the journal sync
92 // operations. Just skip over them.
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070093 if (!IS_UNIT_TEST)
Adriana Kobylakd311bc82016-10-16 09:54:40 -050094 {
Patrick Williamsb01a5b42021-08-28 15:11:45 -050095 static constexpr auto transactionIdVar =
96 std::string_view{"TRANSACTION_ID"};
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070097 // Length of 'TRANSACTION_ID' string.
Patrick Williamsb01a5b42021-08-28 15:11:45 -050098 static constexpr auto transactionIdVarSize = transactionIdVar.size();
William A. Kennington IIIb6b25572021-05-19 17:09:41 -070099 // Length of 'TRANSACTION_ID=' string.
Patrick Williamsb01a5b42021-08-28 15:11:45 -0500100 static constexpr auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500101
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700102 // Flush all the pending log messages into the journal
103 journalSync();
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600104
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700105 sd_journal* j = nullptr;
106 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600107 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600108 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500109 lg2::error("Failed to open journal: {ERROR}", "ERROR",
110 strerror(-rc));
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700111 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
Patrick Williamsb01a5b42021-08-28 15:11:45 -0500134 rc = sd_journal_get_data(j, transactionIdVar.data(),
135 (const void**)&data, &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 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500189 lg2::info("Failed to find metadata: {META_FIELD}", "META_FIELD",
190 metaVarStr);
William A. Kennington IIIb6b25572021-05-19 17:09:41 -0700191 }
192 }
193
194 sd_journal_close(j);
195 }
Matt Spinlerb60e7552019-07-24 15:28:08 -0500196 createEntry(errMsg, errLvl, additionalData);
197}
198
199void Manager::createEntry(std::string errMsg, Entry::Level errLvl,
Matt Spinlerc64b7122020-03-26 10:55:01 -0500200 std::vector<std::string> additionalData,
201 const FFDCEntries& ffdc)
Matt Spinlerb60e7552019-07-24 15:28:08 -0500202{
203 if (!Extensions::disableDefaultLogCaps())
204 {
205 if (errLvl < Entry::sevLowerLimit)
206 {
207 if (realErrors.size() >= ERROR_CAP)
208 {
209 erase(realErrors.front());
210 }
211 }
212 else
213 {
214 if (infoErrors.size() >= ERROR_INFO_CAP)
215 {
216 erase(infoErrors.front());
217 }
218 }
219 }
220
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600221 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500222 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500223 {
224 infoErrors.push_back(entryId);
225 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600226 else
227 {
228 realErrors.push_back(entryId);
229 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600230 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700231 std::chrono::system_clock::now().time_since_epoch())
232 .count();
233 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600234
Patrick Venturef18bf832018-10-26 18:14:00 -0700235 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600236 processMetadata(errMsg, additionalData, objects);
237
Matt Spinlerfb978da2022-01-21 08:42:24 -0600238 auto e = std::make_unique<Entry>(
239 busLog, objPath, entryId,
240 ms, // Milliseconds since 1970
241 errLvl, std::move(errMsg), std::move(additionalData),
242 std::move(objects), fwVersion, getEntrySerializePath(entryId), *this);
243
244 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500245
Matt Spinler601b8112022-01-26 13:18:04 -0600246 if (isQuiesceOnErrorEnabled() && (errLvl < Entry::sevLowerLimit) &&
247 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 }
Patrick Williams45e83522022-07-22 19:26:52 -0500282 catch (const sdbusplus::exception_t& e)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500283 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500284 lg2::error("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
285 e);
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500286 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
Patrick Williams45e83522022-07-22 19:26:52 -0500316void Manager::onEntryResolve(sdbusplus::message_t& msg)
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500317{
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{
Patrick Williamsa144a272021-07-16 17:03:55 -0500340 using Host = sdbusplus::xyz::openbmc_project::State::server::Host;
341
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500342 // First check host state
Patrick Williamsa144a272021-07-16 17:03:55 -0500343 std::variant<Host::HostState> property;
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500344
345 auto method = this->busLog.new_method_call(
346 "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0",
347 "org.freedesktop.DBus.Properties", "Get");
348
349 method.append("xyz.openbmc_project.State.Host", "CurrentHostState");
350
351 try
352 {
353 auto reply = this->busLog.call(method);
354 reply.read(property);
355 }
Patrick Williams45e83522022-07-22 19:26:52 -0500356 catch (const sdbusplus::exception_t& e)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500357 {
358 // Quiescing the host is a "best effort" type function. If unable to
359 // read the host state or it comes back empty, just return.
360 // The boot block object will still be created and the associations to
361 // find the log will be present. Don't want a dependency with
362 // phosphor-state-manager service
Patrick Williams5f285c52021-07-27 21:25:38 -0500363 lg2::info("Error reading QuiesceOnHwError property: {ERROR}", "ERROR",
364 e);
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500365 return;
366 }
367
Patrick Williamsa144a272021-07-16 17:03:55 -0500368 auto hostState = std::get<Host::HostState>(property);
369 if (hostState != Host::HostState::Running)
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500370 {
371 return;
372 }
373
374 auto quiesce = this->busLog.new_method_call(
375 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
376 "org.freedesktop.systemd1.Manager", "StartUnit");
377
Andrew Geissler86a60e92022-04-15 14:36:02 -0500378 quiesce.append("obmc-host-graceful-quiesce@0.target");
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500379 quiesce.append("replace");
380
381 this->busLog.call_noreply(quiesce);
382}
383
Andrew Geissler32874542020-07-09 09:17:03 -0500384void Manager::quiesceOnError(const uint32_t entryId)
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500385{
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500386 // Verify we don't already have this entry blocking
Matt Spinler4a375952022-07-01 11:15:33 -0500387 auto it = find_if(this->blockingErrors.begin(), this->blockingErrors.end(),
388 [&](const std::unique_ptr<Block>& obj) {
389 return obj->entryId == entryId;
390 });
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500391 if (it != this->blockingErrors.end())
392 {
393 // Already recorded so just return
Patrick Williams5f285c52021-07-27 21:25:38 -0500394 lg2::debug(
Andrew Geissler72a1cca2020-09-10 16:49:09 -0500395 "QuiesceOnError set and callout present but entry already logged");
396 return;
397 }
Andrew Geisslere4960ee2020-03-30 14:31:52 -0500398
Patrick Williams5f285c52021-07-27 21:25:38 -0500399 lg2::info("QuiesceOnError set and callout present");
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500400
Patrick Williams2544b412022-10-04 08:41:06 -0500401 auto blockPath = std::string(OBJ_LOGGING) + "/block" +
402 std::to_string(entryId);
Andrew Geissler32874542020-07-09 09:17:03 -0500403 auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId);
Andrew Geissler6a0ef6f2020-04-06 15:06:31 -0500404 this->blockingErrors.push_back(std::move(blockObj));
405
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500406 // Register call back if log is resolved
407 using namespace sdbusplus::bus::match::rules;
Andrew Geissler32874542020-07-09 09:17:03 -0500408 auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Patrick Williams45e83522022-07-22 19:26:52 -0500409 auto callback = std::make_unique<sdbusplus::bus::match_t>(
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500410 this->busLog,
411 propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"),
412 std::bind(std::mem_fn(&Manager::onEntryResolve), this,
413 std::placeholders::_1));
414
415 propChangedEntryCallback.insert(
Andrew Geissler32874542020-07-09 09:17:03 -0500416 std::make_pair(entryId, std::move(callback)));
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500417
Andrew Geisslerf6126a72020-05-08 10:41:09 -0500418 checkAndQuiesceHost();
Andrew Geisslerc0c500e2020-03-26 11:08:56 -0500419}
420
Matt Spinlerc64b7122020-03-26 10:55:01 -0500421void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500422{
423 // Make the association <endpointpath>/<endpointtype> paths
424 std::vector<std::string> assocs;
425 for (const auto& [forwardType, reverseType, endpoint] :
426 entry.associations())
427 {
428 std::string e{endpoint};
429 e += '/' + reverseType;
430 assocs.push_back(e);
431 }
432
433 for (auto& create : Extensions::getCreateFunctions())
434 {
435 try
436 {
437 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500438 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500439 }
Patrick Williams66491c62021-10-06 12:23:37 -0500440 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500441 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500442 lg2::error(
443 "An extension's create function threw an exception: {ERROR}",
444 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500445 }
446 }
447}
448
Patrick Williamsf40323d2021-04-16 15:35:17 -0500449void Manager::processMetadata(const std::string& /*errorName*/,
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600450 const std::vector<std::string>& additionalData,
451 AssociationList& objects) const
452{
453 // additionalData is a list of "metadata=value"
454 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700455 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600456 {
Patrick Venture34438962018-10-30 13:17:37 -0700457 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700458 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600459 {
Patrick Venture34438962018-10-30 13:17:37 -0700460 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600461 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700462 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600463 {
464 (iter->second)(metadata, additionalData, objects);
465 }
466 }
467 }
468}
469
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500470void Manager::checkAndRemoveBlockingError(uint32_t entryId)
471{
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500472 // First look for blocking object and remove
Matt Spinler4a375952022-07-01 11:15:33 -0500473 auto it = find_if(blockingErrors.begin(), blockingErrors.end(),
474 [&](const std::unique_ptr<Block>& obj) {
475 return obj->entryId == entryId;
476 });
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500477 if (it != blockingErrors.end())
478 {
479 blockingErrors.erase(it);
480 }
Andrew Geissler7f6d4bc2020-04-16 14:47:34 -0500481
482 // Now remove the callback looking for the error to be resolved
483 auto resolveFind = propChangedEntryCallback.find(entryId);
484 if (resolveFind != propChangedEntryCallback.end())
485 {
486 propChangedEntryCallback.erase(resolveFind);
487 }
488
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500489 return;
490}
491
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500492void Manager::erase(uint32_t entryId)
493{
Patrick Venture34438962018-10-30 13:17:37 -0700494 auto entryFound = entries.find(entryId);
495 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500496 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500497 for (auto& func : Extensions::getDeleteProhibitedFunctions())
498 {
499 try
500 {
501 bool prohibited = false;
502 func(entryId, prohibited);
503 if (prohibited)
504 {
505 // Future work remains to throw an error here.
506 return;
507 }
508 }
Patrick Williams66491c62021-10-06 12:23:37 -0500509 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500510 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500511 lg2::error("An extension's deleteProhibited function threw an "
512 "exception: {ERROR}",
513 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500514 }
515 }
516
Deepak Kodihalli33887992017-06-13 07:06:49 -0500517 // Delete the persistent representation of this error.
518 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600519 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500520 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600521
Patrick Venturef18bf832018-10-26 18:14:00 -0700522 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600523 auto it = std::find(ids.begin(), ids.end(), id);
524 if (it != ids.end())
525 {
526 ids.erase(it);
527 }
528 };
Patrick Venture34438962018-10-30 13:17:37 -0700529 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500530 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600531 removeId(infoErrors, entryId);
532 }
533 else
534 {
535 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500536 }
Patrick Venture34438962018-10-30 13:17:37 -0700537 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500538
Andrew Geisslerced6e2a2020-04-07 16:15:29 -0500539 checkAndRemoveBlockingError(entryId);
540
Matt Spinler99c2b402019-05-23 14:29:16 -0500541 for (auto& remove : Extensions::getDeleteFunctions())
542 {
543 try
544 {
545 remove(entryId);
546 }
Patrick Williams66491c62021-10-06 12:23:37 -0500547 catch (const std::exception& e)
Matt Spinler99c2b402019-05-23 14:29:16 -0500548 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500549 lg2::error("An extension's delete function threw an exception: "
550 "{ERROR}",
551 "ERROR", e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500552 }
553 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500554 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600555 else
556 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500557 lg2::error("Invalid entry ID ({ID}) to delete", "ID", entryId);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600558 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500559}
560
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500561void Manager::restore()
562{
Patrick Venturef18bf832018-10-26 18:14:00 -0700563 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600564 return id == restoredId;
565 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500566
567 fs::path dir(ERRLOG_PERSIST_PATH);
568 if (!fs::exists(dir) || fs::is_empty(dir))
569 {
570 return;
571 }
572
Patrick Venturef18bf832018-10-26 18:14:00 -0700573 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500574 {
575 auto id = file.path().filename().c_str();
576 auto idNum = std::stol(id);
577 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700578 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500579 if (deserialize(file.path(), *e))
580 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700581 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600582 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500583 {
Matt Spinleref952af2021-08-19 10:23:05 -0500584 e->path(file.path(), true);
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600585 if (e->severity() >= Entry::sevLowerLimit)
586 {
587 infoErrors.push_back(idNum);
588 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600589 else
590 {
591 realErrors.push_back(idNum);
592 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600593
594 entries.insert(std::make_pair(idNum, std::move(e)));
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500595 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600596 else
597 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500598 lg2::error(
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600599 "Failed in sanity check while restoring error entry. "
Patrick Williams5f285c52021-07-27 21:25:38 -0500600 "Ignoring error entry {ID_NUM}/{ENTRY_ID}.",
601 "ID_NUM", idNum, "ENTRY_ID", e->id());
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600602 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500603 }
604 }
605
Lei YU01bf5c42022-05-23 15:33:23 +0800606 if (!entries.empty())
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530607 {
Lei YU01bf5c42022-05-23 15:33:23 +0800608 entryId = entries.rbegin()->first;
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530609 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500610}
611
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500612void Manager::journalSync()
613{
614 bool syncRequested = false;
615 auto fd = -1;
616 auto rc = -1;
617 auto wd = -1;
618 auto bus = sdbusplus::bus::new_default();
619
620 auto start =
621 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
622 .count();
623
Matthew Barth59913892019-08-27 15:43:48 -0500624 // Each time an error log is committed, a request to sync the journal
625 // must occur and block that error log commit until it completes. A 5sec
626 // block is done to allow sufficient time for the journal to be synced.
627 //
628 // Number of loop iterations = 3 for the following reasons:
629 // Iteration #1: Requests a journal sync by killing the journald service.
630 // Iteration #2: Setup an inotify watch to monitor the synced file that
631 // journald updates with the timestamp the last time the
632 // journal was flushed.
633 // Iteration #3: Poll to wait until inotify reports an event which blocks
634 // the error log from being commited until the sync completes.
635 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500636 for (int i = 0; i < maxRetry; i++)
637 {
638 // Read timestamp from synced file
639 constexpr auto syncedPath = "/run/systemd/journal/synced";
640 std::ifstream syncedFile(syncedPath);
641 if (syncedFile.fail())
642 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500643 // If the synced file doesn't exist, a sync request will create it.
644 if (errno != ENOENT)
645 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500646 lg2::error(
647 "Failed to open journal synced file {FILENAME}: {ERROR}",
648 "FILENAME", syncedPath, "ERROR", strerror(errno));
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500649 return;
650 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500651 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500652 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500653 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500654 // Only read the synced file if it exists.
655 // See if a sync happened by now
656 std::string timestampStr;
657 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700658 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500659 if (timestamp >= start)
660 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800661 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500662 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500663 }
664
665 // Let's ask for a sync, but only once
666 if (!syncRequested)
667 {
668 syncRequested = true;
669
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500670 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
671 auto signal = SIGRTMIN + 1;
672
673 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
674 SYSTEMD_INTERFACE, "KillUnit");
675 method.append(JOURNAL_UNIT, "main", signal);
676 bus.call(method);
677 if (method.is_method_error())
678 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500679 lg2::error("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800680 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500681 }
Patrick Williamsa5171972021-04-16 20:10:01 -0500682
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500683 continue;
684 }
685
686 // Let's install the inotify watch, if we didn't do that yet. This watch
687 // monitors the syncedFile for when journald updates it with a newer
688 // timestamp. This means the journal has been flushed.
689 if (fd < 0)
690 {
691 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
692 if (fd < 0)
693 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500694 lg2::error("Failed to create inotify watch: {ERROR}", "ERROR",
695 strerror(errno));
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500696 return;
697 }
698
699 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
700 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
701 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
702 if (wd < 0)
703 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500704 lg2::error("Failed to watch journal directory: {PATH}: {ERROR}",
705 "PATH", JOURNAL_RUN_PATH, "ERROR", strerror(errno));
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500706 close(fd);
707 return;
708 }
709 continue;
710 }
711
712 // Let's wait until inotify reports an event
713 struct pollfd fds = {
Andrew Jefferyeb0aec42021-06-15 11:36:04 +0930714 fd,
715 POLLIN,
716 0,
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500717 };
718 constexpr auto pollTimeout = 5; // 5 seconds
719 rc = poll(&fds, 1, pollTimeout * 1000);
720 if (rc < 0)
721 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500722 lg2::error("Failed to add event: {ERROR}", "ERROR",
723 strerror(errno));
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500724 inotify_rm_watch(fd, wd);
725 close(fd);
726 return;
727 }
728 else if (rc == 0)
729 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500730 lg2::info("Poll timeout ({TIMEOUT}), no new journal synced data",
731 "TIMEOUT", pollTimeout);
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500732 break;
733 }
734
735 // Read from the specified file descriptor until there is no new data,
736 // throwing away everything read since the timestamp will be read at the
737 // beginning of the loop.
738 constexpr auto maxBytes = 64;
739 uint8_t buffer[maxBytes];
740 while (read(fd, buffer, maxBytes) > 0)
741 ;
742 }
743
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500744 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500745 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500746 if (wd != -1)
747 {
748 inotify_rm_watch(fd, wd);
749 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500750 close(fd);
751 }
752
753 return;
754}
755
Matt Spinler1275bd12018-05-01 15:13:53 -0500756std::string Manager::readFWVersion()
757{
Matt Spinlerf61f2922020-06-23 11:32:49 -0500758 auto version = util::getOSReleaseValue("VERSION_ID");
Matt Spinler1275bd12018-05-01 15:13:53 -0500759
Matt Spinlerf61f2922020-06-23 11:32:49 -0500760 if (!version)
Matt Spinler1275bd12018-05-01 15:13:53 -0500761 {
Patrick Williams5f285c52021-07-27 21:25:38 -0500762 lg2::error("Unable to read BMC firmware version");
Matt Spinler1275bd12018-05-01 15:13:53 -0500763 }
764
Matt Spinlerf61f2922020-06-23 11:32:49 -0500765 return version.value_or("");
Matt Spinler1275bd12018-05-01 15:13:53 -0500766}
767
Matt Spinler3fb83b32019-07-26 11:22:44 -0500768void Manager::create(const std::string& message, Entry::Level severity,
769 const std::map<std::string, std::string>& additionalData)
770{
771 // Convert the map into a vector of "key=value" strings
772 std::vector<std::string> ad;
773 metadata::associations::combine(additionalData, ad);
774
775 createEntry(message, severity, ad);
776}
777
Matt Spinlerc64b7122020-03-26 10:55:01 -0500778void Manager::createWithFFDC(
779 const std::string& message, Entry::Level severity,
780 const std::map<std::string, std::string>& additionalData,
781 const FFDCEntries& ffdc)
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, ffdc);
788}
789
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500790} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600791} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700792} // namespace phosphor