blob: bdb2e2ef70adb4aff77838de0a00b704fdc131a0 [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;
Deepak Kodihallia87c1572017-02-28 07:40:34 -060033extern const std::map<metadata::Metadata,
Patrick Venturef18bf832018-10-26 18:14:00 -070034 std::function<metadata::associations::Type>>
35 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050036
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060037namespace phosphor
38{
39namespace logging
40{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050041namespace internal
42{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050043inline auto getLevel(const std::string& errMsg)
44{
45 auto reqLevel = Entry::Level::Error; // Default to Error
46
47 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050048 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050049 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050050 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050051 }
52
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050053 return reqLevel;
54}
55
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050056int Manager::getRealErrSize()
57{
58 return realErrors.size();
59}
60
61int Manager::getInfoErrSize()
62{
63 return infoErrors.size();
64}
65
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050066void Manager::commit(uint64_t transactionId, std::string errMsg)
67{
68 auto level = getLevel(errMsg);
69 _commit(transactionId, std::move(errMsg), level);
70}
71
72void Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
73 uint32_t errLvl)
74{
75 _commit(transactionId, std::move(errMsg),
76 static_cast<Entry::Level>(errLvl));
77}
78
79void Manager::_commit(uint64_t transactionId, std::string&& errMsg,
80 Entry::Level errLvl)
81{
Adriana Kobylak7298dc22017-01-24 12:21:50 -060082 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -060083 // Length of 'TRANSACTION_ID' string.
Patrick Venture30047bf2018-11-01 18:52:15 -070084 constexpr const auto transactionIdVarSize = std::strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -060085 // Length of 'TRANSACTION_ID=' string.
86 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050087
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050088 // Flush all the pending log messages into the journal
89 journalSync();
Adriana Kobylakcfd9a7d2017-06-07 11:57:31 -050090
Patrick Venturef18bf832018-10-26 18:14:00 -070091 sd_journal* j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060092 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050093 if (rc < 0)
94 {
Patrick Venturef18bf832018-10-26 18:14:00 -070095 logging::log<logging::level::ERR>(
96 "Failed to open journal",
97 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060098 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -050099 }
100
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600101 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -0600102 std::set<std::string> metalist;
103 auto metamap = g_errMetaMap.find(errMsg);
104 if (metamap != g_errMetaMap.end())
105 {
106 metalist.insert(metamap->second.begin(), metamap->second.end());
107 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600108
Patrick Venturef18bf832018-10-26 18:14:00 -0700109 // Add _PID field information in AdditionalData.
Jayanth Othayothdb18ebe2017-09-04 00:48:02 -0500110 metalist.insert("_PID");
111
Tom Joseph7a33ee42017-07-25 00:04:20 +0530112 std::vector<std::string> additionalData;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600113
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600114 // Read the journal from the end to get the most recent entry first.
115 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
116 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600117 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700118 const char* data = nullptr;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600119 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600120
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600121 // Look for the transaction id metadata variable
Patrick Venturef18bf832018-10-26 18:14:00 -0700122 rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data,
123 &length);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600124 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600125 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600126 // This journal entry does not have the TRANSACTION_ID
127 // metadata variable.
128 continue;
129 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600130
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600131 // journald does not guarantee that sd_journal_get_data() returns NULL
132 // terminated strings, so need to specify the size to use to compare,
133 // use the returned length instead of anything that relies on NULL
134 // terminators like strlen().
135 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
136 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
137 // 'data + transactionIdVarOffset' will be in the form of '1234'.
138 // 'length - transactionIdVarOffset' will be the length of '1234'.
139 if ((length <= (transactionIdVarOffset)) ||
Patrick Venturef18bf832018-10-26 18:14:00 -0700140 (transactionIdStr.compare(0, transactionIdStr.size(),
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600141 data + transactionIdVarOffset,
142 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600143 {
144 // The value of the TRANSACTION_ID metadata is not the requested
145 // transaction id number.
146 continue;
147 }
148
149 // Search for all metadata variables in the current journal entry.
150 for (auto i = metalist.cbegin(); i != metalist.cend();)
151 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700152 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
153 &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600154 if (rc < 0)
155 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600156 // Metadata variable not found, check next metadata variable.
157 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600158 continue;
159 }
160
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600161 // Metadata variable found, save it and remove it from the set.
162 additionalData.emplace_back(data, length);
163 i = metalist.erase(i);
164 }
165 if (metalist.empty())
166 {
167 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600168 break;
169 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600170 }
171 if (!metalist.empty())
172 {
173 // Not all the metadata variables were found in the journal.
174 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600175 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700176 logging::log<logging::level::INFO>(
177 "Failed to find metadata",
178 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600179 }
180 }
181
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500182 sd_journal_close(j);
183
Matt Spinlerb60e7552019-07-24 15:28:08 -0500184 createEntry(errMsg, errLvl, additionalData);
185}
186
187void Manager::createEntry(std::string errMsg, Entry::Level errLvl,
Matt Spinlerc64b7122020-03-26 10:55:01 -0500188 std::vector<std::string> additionalData,
189 const FFDCEntries& ffdc)
Matt Spinlerb60e7552019-07-24 15:28:08 -0500190{
191 if (!Extensions::disableDefaultLogCaps())
192 {
193 if (errLvl < Entry::sevLowerLimit)
194 {
195 if (realErrors.size() >= ERROR_CAP)
196 {
197 erase(realErrors.front());
198 }
199 }
200 else
201 {
202 if (infoErrors.size() >= ERROR_INFO_CAP)
203 {
204 erase(infoErrors.front());
205 }
206 }
207 }
208
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600209 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500210 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500211 {
212 infoErrors.push_back(entryId);
213 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600214 else
215 {
216 realErrors.push_back(entryId);
217 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600218 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700219 std::chrono::system_clock::now().time_since_epoch())
220 .count();
221 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600222
Patrick Venturef18bf832018-10-26 18:14:00 -0700223 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600224 processMetadata(errMsg, additionalData, objects);
225
Patrick Venturef18bf832018-10-26 18:14:00 -0700226 auto e = std::make_unique<Entry>(busLog, objPath, entryId,
227 ms, // Milliseconds since 1970
228 errLvl, std::move(errMsg),
229 std::move(additionalData),
230 std::move(objects), fwVersion, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500231 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500232
Matt Spinlerc64b7122020-03-26 10:55:01 -0500233 doExtensionLogCreate(*e, ffdc);
234
235 // Note: No need to close the file descriptors in the FFDC.
Matt Spinler99c2b402019-05-23 14:29:16 -0500236
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500237 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500238}
239
Matt Spinlerc64b7122020-03-26 10:55:01 -0500240void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc)
Matt Spinler99c2b402019-05-23 14:29:16 -0500241{
242 // Make the association <endpointpath>/<endpointtype> paths
243 std::vector<std::string> assocs;
244 for (const auto& [forwardType, reverseType, endpoint] :
245 entry.associations())
246 {
247 std::string e{endpoint};
248 e += '/' + reverseType;
249 assocs.push_back(e);
250 }
251
252 for (auto& create : Extensions::getCreateFunctions())
253 {
254 try
255 {
256 create(entry.message(), entry.id(), entry.timestamp(),
Matt Spinlerc64b7122020-03-26 10:55:01 -0500257 entry.severity(), entry.additionalData(), assocs, ffdc);
Matt Spinler99c2b402019-05-23 14:29:16 -0500258 }
259 catch (std::exception& e)
260 {
261 log<level::ERR>("An extension's create function threw an exception",
262 phosphor::logging::entry("ERROR=%s", e.what()));
263 }
264 }
265}
266
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600267void Manager::processMetadata(const std::string& errorName,
268 const std::vector<std::string>& additionalData,
269 AssociationList& objects) const
270{
271 // additionalData is a list of "metadata=value"
272 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700273 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600274 {
Patrick Venture34438962018-10-30 13:17:37 -0700275 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700276 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600277 {
Patrick Venture34438962018-10-30 13:17:37 -0700278 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600279 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700280 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600281 {
282 (iter->second)(metadata, additionalData, objects);
283 }
284 }
285 }
286}
287
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500288void Manager::erase(uint32_t entryId)
289{
Patrick Venture34438962018-10-30 13:17:37 -0700290 auto entryFound = entries.find(entryId);
291 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500292 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500293 for (auto& func : Extensions::getDeleteProhibitedFunctions())
294 {
295 try
296 {
297 bool prohibited = false;
298 func(entryId, prohibited);
299 if (prohibited)
300 {
301 // Future work remains to throw an error here.
302 return;
303 }
304 }
305 catch (std::exception& e)
306 {
307 log<level::ERR>(
308 "An extension's deleteProhibited function threw "
309 "an exception",
310 entry("ERROR=%s", e.what()));
311 }
312 }
313
Deepak Kodihalli33887992017-06-13 07:06:49 -0500314 // Delete the persistent representation of this error.
315 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600316 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500317 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600318
Patrick Venturef18bf832018-10-26 18:14:00 -0700319 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600320 auto it = std::find(ids.begin(), ids.end(), id);
321 if (it != ids.end())
322 {
323 ids.erase(it);
324 }
325 };
Patrick Venture34438962018-10-30 13:17:37 -0700326 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500327 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600328 removeId(infoErrors, entryId);
329 }
330 else
331 {
332 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500333 }
Patrick Venture34438962018-10-30 13:17:37 -0700334 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500335
336 for (auto& remove : Extensions::getDeleteFunctions())
337 {
338 try
339 {
340 remove(entryId);
341 }
342 catch (std::exception& e)
343 {
344 log<level::ERR>("An extension's delete function threw an "
345 "exception",
346 entry("ERROR=%s", e.what()));
347 }
348 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500349 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600350 else
351 {
352 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700353 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600354 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500355}
356
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500357void Manager::restore()
358{
Patrick Venturef18bf832018-10-26 18:14:00 -0700359 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600360 return id == restoredId;
361 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500362 std::vector<uint32_t> errorIds;
363
364 fs::path dir(ERRLOG_PERSIST_PATH);
365 if (!fs::exists(dir) || fs::is_empty(dir))
366 {
367 return;
368 }
369
Patrick Venturef18bf832018-10-26 18:14:00 -0700370 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500371 {
372 auto id = file.path().filename().c_str();
373 auto idNum = std::stol(id);
374 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700375 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500376 if (deserialize(file.path(), *e))
377 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700378 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600379 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500380 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600381 e->emit_object_added();
382 if (e->severity() >= Entry::sevLowerLimit)
383 {
384 infoErrors.push_back(idNum);
385 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600386 else
387 {
388 realErrors.push_back(idNum);
389 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600390
391 entries.insert(std::make_pair(idNum, std::move(e)));
392 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500393 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600394 else
395 {
396 logging::log<logging::level::ERR>(
397 "Failed in sanity check while restoring error entry. "
398 "Ignoring error entry",
399 logging::entry("ID_NUM=%d", idNum),
400 logging::entry("ENTRY_ID=%d", e->id()));
401 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500402 }
403 }
404
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530405 if (!errorIds.empty())
406 {
407 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
408 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500409}
410
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500411void Manager::journalSync()
412{
413 bool syncRequested = false;
414 auto fd = -1;
415 auto rc = -1;
416 auto wd = -1;
417 auto bus = sdbusplus::bus::new_default();
418
419 auto start =
420 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
421 .count();
422
Matthew Barth59913892019-08-27 15:43:48 -0500423 // Each time an error log is committed, a request to sync the journal
424 // must occur and block that error log commit until it completes. A 5sec
425 // block is done to allow sufficient time for the journal to be synced.
426 //
427 // Number of loop iterations = 3 for the following reasons:
428 // Iteration #1: Requests a journal sync by killing the journald service.
429 // Iteration #2: Setup an inotify watch to monitor the synced file that
430 // journald updates with the timestamp the last time the
431 // journal was flushed.
432 // Iteration #3: Poll to wait until inotify reports an event which blocks
433 // the error log from being commited until the sync completes.
434 constexpr auto maxRetry = 3;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500435 for (int i = 0; i < maxRetry; i++)
436 {
437 // Read timestamp from synced file
438 constexpr auto syncedPath = "/run/systemd/journal/synced";
439 std::ifstream syncedFile(syncedPath);
440 if (syncedFile.fail())
441 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500442 // If the synced file doesn't exist, a sync request will create it.
443 if (errno != ENOENT)
444 {
445 log<level::ERR>("Failed to open journal synced file",
446 entry("FILENAME=%s", syncedPath),
447 entry("ERRNO=%d", errno));
448 return;
449 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500450 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500451 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500452 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500453 // Only read the synced file if it exists.
454 // See if a sync happened by now
455 std::string timestampStr;
456 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700457 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500458 if (timestamp >= start)
459 {
Troy Leeb7f392a2019-11-19 14:34:56 +0800460 break;
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500461 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500462 }
463
464 // Let's ask for a sync, but only once
465 if (!syncRequested)
466 {
467 syncRequested = true;
468
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500469 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
470 auto signal = SIGRTMIN + 1;
471
472 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
473 SYSTEMD_INTERFACE, "KillUnit");
474 method.append(JOURNAL_UNIT, "main", signal);
475 bus.call(method);
476 if (method.is_method_error())
477 {
478 log<level::ERR>("Failed to kill journal service");
Troy Leeb7f392a2019-11-19 14:34:56 +0800479 break;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500480 }
481 continue;
482 }
483
484 // Let's install the inotify watch, if we didn't do that yet. This watch
485 // monitors the syncedFile for when journald updates it with a newer
486 // timestamp. This means the journal has been flushed.
487 if (fd < 0)
488 {
489 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
490 if (fd < 0)
491 {
492 log<level::ERR>("Failed to create inotify watch",
493 entry("ERRNO=%d", errno));
494 return;
495 }
496
497 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
498 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
499 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
500 if (wd < 0)
501 {
502 log<level::ERR>("Failed to watch journal directory",
503 entry("PATH=%s", JOURNAL_RUN_PATH),
504 entry("ERRNO=%d", errno));
505 close(fd);
506 return;
507 }
508 continue;
509 }
510
511 // Let's wait until inotify reports an event
512 struct pollfd fds = {
513 .fd = fd,
514 .events = POLLIN,
515 };
516 constexpr auto pollTimeout = 5; // 5 seconds
517 rc = poll(&fds, 1, pollTimeout * 1000);
518 if (rc < 0)
519 {
520 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
521 entry("ERR=%s", strerror(-rc)));
522 inotify_rm_watch(fd, wd);
523 close(fd);
524 return;
525 }
526 else if (rc == 0)
527 {
528 log<level::INFO>("Poll timeout, no new journal synced data",
529 entry("TIMEOUT=%d", pollTimeout));
530 break;
531 }
532
533 // Read from the specified file descriptor until there is no new data,
534 // throwing away everything read since the timestamp will be read at the
535 // beginning of the loop.
536 constexpr auto maxBytes = 64;
537 uint8_t buffer[maxBytes];
538 while (read(fd, buffer, maxBytes) > 0)
539 ;
540 }
541
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500542 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500543 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500544 if (wd != -1)
545 {
546 inotify_rm_watch(fd, wd);
547 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500548 close(fd);
549 }
550
551 return;
552}
553
Matt Spinler1275bd12018-05-01 15:13:53 -0500554std::string Manager::readFWVersion()
555{
556 std::string version;
557 std::ifstream versionFile{BMC_VERSION_FILE};
558 std::string line;
559 static constexpr auto VERSION_ID = "VERSION_ID=";
560
561 while (std::getline(versionFile, line))
562 {
563 if (line.find(VERSION_ID) != std::string::npos)
564 {
565 auto pos = line.find_first_of('"') + 1;
566 version = line.substr(pos, line.find_last_of('"') - pos);
567 break;
568 }
569 }
570
571 if (version.empty())
572 {
573 log<level::ERR>("Unable to read BMC firmware version");
574 }
575
576 return version;
577}
578
Matt Spinler3fb83b32019-07-26 11:22:44 -0500579void Manager::create(const std::string& message, Entry::Level severity,
580 const std::map<std::string, std::string>& additionalData)
581{
582 // Convert the map into a vector of "key=value" strings
583 std::vector<std::string> ad;
584 metadata::associations::combine(additionalData, ad);
585
586 createEntry(message, severity, ad);
587}
588
Matt Spinlerc64b7122020-03-26 10:55:01 -0500589void Manager::createWithFFDC(
590 const std::string& message, Entry::Level severity,
591 const std::map<std::string, std::string>& additionalData,
592 const FFDCEntries& ffdc)
593{
594 // Convert the map into a vector of "key=value" strings
595 std::vector<std::string> ad;
596 metadata::associations::combine(additionalData, ad);
597
598 createEntry(message, severity, ad, ffdc);
599}
600
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500601} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600602} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700603} // namespace phosphor