blob: 0f4e57c1fdd7fe6a5b8263da74b0f91ed381abea [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{
Matt Spinler99c2b402019-05-23 14:29:16 -050082 if (!Extensions::disableDefaultLogCaps())
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050083 {
Matt Spinler99c2b402019-05-23 14:29:16 -050084 if (errLvl < Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050085 {
Matt Spinler99c2b402019-05-23 14:29:16 -050086 if (realErrors.size() >= ERROR_CAP)
87 {
88 erase(realErrors.front());
89 }
90 }
91 else
92 {
93 if (infoErrors.size() >= ERROR_INFO_CAP)
94 {
95 erase(infoErrors.front());
96 }
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050097 }
98 }
Matt Spinler99c2b402019-05-23 14:29:16 -050099
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600100 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600101 // Length of 'TRANSACTION_ID' string.
Patrick Venture30047bf2018-11-01 18:52:15 -0700102 constexpr const auto transactionIdVarSize = std::strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600103 // Length of 'TRANSACTION_ID=' string.
104 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500105
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500106 // Flush all the pending log messages into the journal
107 journalSync();
Adriana Kobylakcfd9a7d2017-06-07 11:57:31 -0500108
Patrick Venturef18bf832018-10-26 18:14:00 -0700109 sd_journal* j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600110 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500111 if (rc < 0)
112 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700113 logging::log<logging::level::ERR>(
114 "Failed to open journal",
115 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600116 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500117 }
118
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600119 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -0600120 std::set<std::string> metalist;
121 auto metamap = g_errMetaMap.find(errMsg);
122 if (metamap != g_errMetaMap.end())
123 {
124 metalist.insert(metamap->second.begin(), metamap->second.end());
125 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600126
Patrick Venturef18bf832018-10-26 18:14:00 -0700127 // Add _PID field information in AdditionalData.
Jayanth Othayothdb18ebe2017-09-04 00:48:02 -0500128 metalist.insert("_PID");
129
Tom Joseph7a33ee42017-07-25 00:04:20 +0530130 std::vector<std::string> additionalData;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600131
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600132 // Read the journal from the end to get the most recent entry first.
133 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
134 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600135 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700136 const char* data = nullptr;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600137 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600138
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600139 // Look for the transaction id metadata variable
Patrick Venturef18bf832018-10-26 18:14:00 -0700140 rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data,
141 &length);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600142 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600143 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600144 // This journal entry does not have the TRANSACTION_ID
145 // metadata variable.
146 continue;
147 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600148
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600149 // journald does not guarantee that sd_journal_get_data() returns NULL
150 // terminated strings, so need to specify the size to use to compare,
151 // use the returned length instead of anything that relies on NULL
152 // terminators like strlen().
153 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
154 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
155 // 'data + transactionIdVarOffset' will be in the form of '1234'.
156 // 'length - transactionIdVarOffset' will be the length of '1234'.
157 if ((length <= (transactionIdVarOffset)) ||
Patrick Venturef18bf832018-10-26 18:14:00 -0700158 (transactionIdStr.compare(0, transactionIdStr.size(),
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600159 data + transactionIdVarOffset,
160 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600161 {
162 // The value of the TRANSACTION_ID metadata is not the requested
163 // transaction id number.
164 continue;
165 }
166
167 // Search for all metadata variables in the current journal entry.
168 for (auto i = metalist.cbegin(); i != metalist.cend();)
169 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700170 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
171 &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600172 if (rc < 0)
173 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600174 // Metadata variable not found, check next metadata variable.
175 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600176 continue;
177 }
178
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600179 // Metadata variable found, save it and remove it from the set.
180 additionalData.emplace_back(data, length);
181 i = metalist.erase(i);
182 }
183 if (metalist.empty())
184 {
185 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600186 break;
187 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600188 }
189 if (!metalist.empty())
190 {
191 // Not all the metadata variables were found in the journal.
192 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600193 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700194 logging::log<logging::level::INFO>(
195 "Failed to find metadata",
196 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600197 }
198 }
199
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500200 sd_journal_close(j);
201
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600202 // Create error Entry dbus object
203 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500204 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500205 {
206 infoErrors.push_back(entryId);
207 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600208 else
209 {
210 realErrors.push_back(entryId);
211 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600212 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700213 std::chrono::system_clock::now().time_since_epoch())
214 .count();
215 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600216
Patrick Venturef18bf832018-10-26 18:14:00 -0700217 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600218 processMetadata(errMsg, additionalData, objects);
219
Patrick Venturef18bf832018-10-26 18:14:00 -0700220 auto e = std::make_unique<Entry>(busLog, objPath, entryId,
221 ms, // Milliseconds since 1970
222 errLvl, std::move(errMsg),
223 std::move(additionalData),
224 std::move(objects), fwVersion, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500225 serialize(*e);
Matt Spinler99c2b402019-05-23 14:29:16 -0500226
227 doExtensionLogCreate(*e);
228
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500229 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500230}
231
Matt Spinler99c2b402019-05-23 14:29:16 -0500232void Manager::doExtensionLogCreate(const Entry& entry)
233{
234 // Make the association <endpointpath>/<endpointtype> paths
235 std::vector<std::string> assocs;
236 for (const auto& [forwardType, reverseType, endpoint] :
237 entry.associations())
238 {
239 std::string e{endpoint};
240 e += '/' + reverseType;
241 assocs.push_back(e);
242 }
243
244 for (auto& create : Extensions::getCreateFunctions())
245 {
246 try
247 {
248 create(entry.message(), entry.id(), entry.timestamp(),
249 entry.severity(), entry.additionalData(), assocs);
250 }
251 catch (std::exception& e)
252 {
253 log<level::ERR>("An extension's create function threw an exception",
254 phosphor::logging::entry("ERROR=%s", e.what()));
255 }
256 }
257}
258
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600259void Manager::processMetadata(const std::string& errorName,
260 const std::vector<std::string>& additionalData,
261 AssociationList& objects) const
262{
263 // additionalData is a list of "metadata=value"
264 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700265 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600266 {
Patrick Venture34438962018-10-30 13:17:37 -0700267 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700268 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600269 {
Patrick Venture34438962018-10-30 13:17:37 -0700270 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600271 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700272 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600273 {
274 (iter->second)(metadata, additionalData, objects);
275 }
276 }
277 }
278}
279
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500280void Manager::erase(uint32_t entryId)
281{
Patrick Venture34438962018-10-30 13:17:37 -0700282 auto entryFound = entries.find(entryId);
283 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500284 {
Matt Spinler99c2b402019-05-23 14:29:16 -0500285 for (auto& func : Extensions::getDeleteProhibitedFunctions())
286 {
287 try
288 {
289 bool prohibited = false;
290 func(entryId, prohibited);
291 if (prohibited)
292 {
293 // Future work remains to throw an error here.
294 return;
295 }
296 }
297 catch (std::exception& e)
298 {
299 log<level::ERR>(
300 "An extension's deleteProhibited function threw "
301 "an exception",
302 entry("ERROR=%s", e.what()));
303 }
304 }
305
Deepak Kodihalli33887992017-06-13 07:06:49 -0500306 // Delete the persistent representation of this error.
307 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600308 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500309 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600310
Patrick Venturef18bf832018-10-26 18:14:00 -0700311 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600312 auto it = std::find(ids.begin(), ids.end(), id);
313 if (it != ids.end())
314 {
315 ids.erase(it);
316 }
317 };
Patrick Venture34438962018-10-30 13:17:37 -0700318 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500319 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600320 removeId(infoErrors, entryId);
321 }
322 else
323 {
324 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500325 }
Patrick Venture34438962018-10-30 13:17:37 -0700326 entries.erase(entryFound);
Matt Spinler99c2b402019-05-23 14:29:16 -0500327
328 for (auto& remove : Extensions::getDeleteFunctions())
329 {
330 try
331 {
332 remove(entryId);
333 }
334 catch (std::exception& e)
335 {
336 log<level::ERR>("An extension's delete function threw an "
337 "exception",
338 entry("ERROR=%s", e.what()));
339 }
340 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500341 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600342 else
343 {
344 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700345 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600346 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500347}
348
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500349void Manager::restore()
350{
Patrick Venturef18bf832018-10-26 18:14:00 -0700351 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600352 return id == restoredId;
353 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500354 std::vector<uint32_t> errorIds;
355
356 fs::path dir(ERRLOG_PERSIST_PATH);
357 if (!fs::exists(dir) || fs::is_empty(dir))
358 {
359 return;
360 }
361
Patrick Venturef18bf832018-10-26 18:14:00 -0700362 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500363 {
364 auto id = file.path().filename().c_str();
365 auto idNum = std::stol(id);
366 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700367 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500368 if (deserialize(file.path(), *e))
369 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700370 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600371 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500372 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600373 e->emit_object_added();
374 if (e->severity() >= Entry::sevLowerLimit)
375 {
376 infoErrors.push_back(idNum);
377 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600378 else
379 {
380 realErrors.push_back(idNum);
381 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600382
383 entries.insert(std::make_pair(idNum, std::move(e)));
384 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500385 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600386 else
387 {
388 logging::log<logging::level::ERR>(
389 "Failed in sanity check while restoring error entry. "
390 "Ignoring error entry",
391 logging::entry("ID_NUM=%d", idNum),
392 logging::entry("ENTRY_ID=%d", e->id()));
393 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500394 }
395 }
396
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530397 if (!errorIds.empty())
398 {
399 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
400 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500401}
402
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500403void Manager::journalSync()
404{
405 bool syncRequested = false;
406 auto fd = -1;
407 auto rc = -1;
408 auto wd = -1;
409 auto bus = sdbusplus::bus::new_default();
410
411 auto start =
412 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
413 .count();
414
415 constexpr auto maxRetry = 2;
416 for (int i = 0; i < maxRetry; i++)
417 {
418 // Read timestamp from synced file
419 constexpr auto syncedPath = "/run/systemd/journal/synced";
420 std::ifstream syncedFile(syncedPath);
421 if (syncedFile.fail())
422 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500423 // If the synced file doesn't exist, a sync request will create it.
424 if (errno != ENOENT)
425 {
426 log<level::ERR>("Failed to open journal synced file",
427 entry("FILENAME=%s", syncedPath),
428 entry("ERRNO=%d", errno));
429 return;
430 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500431 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500432 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500433 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500434 // Only read the synced file if it exists.
435 // See if a sync happened by now
436 std::string timestampStr;
437 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700438 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500439 if (timestamp >= start)
440 {
441 return;
442 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500443 }
444
445 // Let's ask for a sync, but only once
446 if (!syncRequested)
447 {
448 syncRequested = true;
449
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500450 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
451 auto signal = SIGRTMIN + 1;
452
453 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
454 SYSTEMD_INTERFACE, "KillUnit");
455 method.append(JOURNAL_UNIT, "main", signal);
456 bus.call(method);
457 if (method.is_method_error())
458 {
459 log<level::ERR>("Failed to kill journal service");
460 return;
461 }
462 continue;
463 }
464
465 // Let's install the inotify watch, if we didn't do that yet. This watch
466 // monitors the syncedFile for when journald updates it with a newer
467 // timestamp. This means the journal has been flushed.
468 if (fd < 0)
469 {
470 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
471 if (fd < 0)
472 {
473 log<level::ERR>("Failed to create inotify watch",
474 entry("ERRNO=%d", errno));
475 return;
476 }
477
478 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
479 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
480 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
481 if (wd < 0)
482 {
483 log<level::ERR>("Failed to watch journal directory",
484 entry("PATH=%s", JOURNAL_RUN_PATH),
485 entry("ERRNO=%d", errno));
486 close(fd);
487 return;
488 }
489 continue;
490 }
491
492 // Let's wait until inotify reports an event
493 struct pollfd fds = {
494 .fd = fd,
495 .events = POLLIN,
496 };
497 constexpr auto pollTimeout = 5; // 5 seconds
498 rc = poll(&fds, 1, pollTimeout * 1000);
499 if (rc < 0)
500 {
501 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
502 entry("ERR=%s", strerror(-rc)));
503 inotify_rm_watch(fd, wd);
504 close(fd);
505 return;
506 }
507 else if (rc == 0)
508 {
509 log<level::INFO>("Poll timeout, no new journal synced data",
510 entry("TIMEOUT=%d", pollTimeout));
511 break;
512 }
513
514 // Read from the specified file descriptor until there is no new data,
515 // throwing away everything read since the timestamp will be read at the
516 // beginning of the loop.
517 constexpr auto maxBytes = 64;
518 uint8_t buffer[maxBytes];
519 while (read(fd, buffer, maxBytes) > 0)
520 ;
521 }
522
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500523 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500524 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500525 if (wd != -1)
526 {
527 inotify_rm_watch(fd, wd);
528 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500529 close(fd);
530 }
531
532 return;
533}
534
Matt Spinler1275bd12018-05-01 15:13:53 -0500535std::string Manager::readFWVersion()
536{
537 std::string version;
538 std::ifstream versionFile{BMC_VERSION_FILE};
539 std::string line;
540 static constexpr auto VERSION_ID = "VERSION_ID=";
541
542 while (std::getline(versionFile, line))
543 {
544 if (line.find(VERSION_ID) != std::string::npos)
545 {
546 auto pos = line.find_first_of('"') + 1;
547 version = line.substr(pos, line.find_last_of('"') - pos);
548 break;
549 }
550 }
551
552 if (version.empty())
553 {
554 log<level::ERR>("Unable to read BMC firmware version");
555 }
556
557 return version;
558}
559
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500560} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600561} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700562} // namespace phosphor