blob: 3dc14d71118776c511043ebe61f4b9b733ae318f [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"
8
Adriana Kobylak5f4247f2018-03-15 10:27:05 -05009#include <poll.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050010#include <sys/inotify.h>
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050011#include <systemd/sd-bus.h>
Adriana Kobylakd311bc82016-10-16 09:54:40 -050012#include <systemd/sd-journal.h>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050013#include <unistd.h>
Patrick Venturef18bf832018-10-26 18:14:00 -070014
15#include <chrono>
16#include <cstdio>
Patrick Venture30047bf2018-11-01 18:52:15 -070017#include <cstring>
Patrick Venturef18bf832018-10-26 18:14:00 -070018#include <fstream>
Patrick Venture30047bf2018-11-01 18:52:15 -070019#include <functional>
Patrick Venturef18bf832018-10-26 18:14:00 -070020#include <future>
21#include <iostream>
Patrick Venture30047bf2018-11-01 18:52:15 -070022#include <map>
Saqib Khan2bb15192017-02-13 13:19:55 -060023#include <phosphor-logging/log.hpp>
Patrick Venturef18bf832018-10-26 18:14:00 -070024#include <sdbusplus/vtable.hpp>
25#include <set>
26#include <string>
27#include <vector>
Deepak Kodihallia87c1572017-02-28 07:40:34 -060028
29using namespace phosphor::logging;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050030using namespace std::chrono;
Deepak Kodihallia87c1572017-02-28 07:40:34 -060031extern const std::map<metadata::Metadata,
Patrick Venturef18bf832018-10-26 18:14:00 -070032 std::function<metadata::associations::Type>>
33 meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050034
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060035namespace phosphor
36{
37namespace logging
38{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050039namespace internal
40{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050041inline auto getLevel(const std::string& errMsg)
42{
43 auto reqLevel = Entry::Level::Error; // Default to Error
44
45 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050046 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050047 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050048 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050049 }
50
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050051 return reqLevel;
52}
53
Nagaraju Goruganti477b7312018-06-25 23:28:58 -050054int Manager::getRealErrSize()
55{
56 return realErrors.size();
57}
58
59int Manager::getInfoErrSize()
60{
61 return infoErrors.size();
62}
63
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050064void Manager::commit(uint64_t transactionId, std::string errMsg)
65{
66 auto level = getLevel(errMsg);
67 _commit(transactionId, std::move(errMsg), level);
68}
69
70void Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
71 uint32_t errLvl)
72{
73 _commit(transactionId, std::move(errMsg),
74 static_cast<Entry::Level>(errLvl));
75}
76
77void Manager::_commit(uint64_t transactionId, std::string&& errMsg,
78 Entry::Level errLvl)
79{
80 if (errLvl < Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050081 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -060082 if (realErrors.size() >= ERROR_CAP)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050083 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -060084 erase(realErrors.front());
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050085 }
86 }
87 else
88 {
89 if (infoErrors.size() >= ERROR_INFO_CAP)
90 {
91 erase(infoErrors.front());
92 }
93 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -060094 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -060095 // Length of 'TRANSACTION_ID' string.
Patrick Venture30047bf2018-11-01 18:52:15 -070096 constexpr const auto transactionIdVarSize = std::strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -060097 // Length of 'TRANSACTION_ID=' string.
98 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050099
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500100 // Flush all the pending log messages into the journal
101 journalSync();
Adriana Kobylakcfd9a7d2017-06-07 11:57:31 -0500102
Patrick Venturef18bf832018-10-26 18:14:00 -0700103 sd_journal* j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600104 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500105 if (rc < 0)
106 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700107 logging::log<logging::level::ERR>(
108 "Failed to open journal",
109 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600110 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500111 }
112
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600113 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -0600114 std::set<std::string> metalist;
115 auto metamap = g_errMetaMap.find(errMsg);
116 if (metamap != g_errMetaMap.end())
117 {
118 metalist.insert(metamap->second.begin(), metamap->second.end());
119 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600120
Patrick Venturef18bf832018-10-26 18:14:00 -0700121 // Add _PID field information in AdditionalData.
Jayanth Othayothdb18ebe2017-09-04 00:48:02 -0500122 metalist.insert("_PID");
123
Tom Joseph7a33ee42017-07-25 00:04:20 +0530124 std::vector<std::string> additionalData;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600125
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600126 // Read the journal from the end to get the most recent entry first.
127 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
128 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600129 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700130 const char* data = nullptr;
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600131 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600132
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600133 // Look for the transaction id metadata variable
Patrick Venturef18bf832018-10-26 18:14:00 -0700134 rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data,
135 &length);
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600136 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600137 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600138 // This journal entry does not have the TRANSACTION_ID
139 // metadata variable.
140 continue;
141 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600142
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600143 // journald does not guarantee that sd_journal_get_data() returns NULL
144 // terminated strings, so need to specify the size to use to compare,
145 // use the returned length instead of anything that relies on NULL
146 // terminators like strlen().
147 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
148 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
149 // 'data + transactionIdVarOffset' will be in the form of '1234'.
150 // 'length - transactionIdVarOffset' will be the length of '1234'.
151 if ((length <= (transactionIdVarOffset)) ||
Patrick Venturef18bf832018-10-26 18:14:00 -0700152 (transactionIdStr.compare(0, transactionIdStr.size(),
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600153 data + transactionIdVarOffset,
154 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600155 {
156 // The value of the TRANSACTION_ID metadata is not the requested
157 // transaction id number.
158 continue;
159 }
160
161 // Search for all metadata variables in the current journal entry.
162 for (auto i = metalist.cbegin(); i != metalist.cend();)
163 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700164 rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data,
165 &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600166 if (rc < 0)
167 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600168 // Metadata variable not found, check next metadata variable.
169 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600170 continue;
171 }
172
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600173 // Metadata variable found, save it and remove it from the set.
174 additionalData.emplace_back(data, length);
175 i = metalist.erase(i);
176 }
177 if (metalist.empty())
178 {
179 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600180 break;
181 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600182 }
183 if (!metalist.empty())
184 {
185 // Not all the metadata variables were found in the journal.
186 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600187 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700188 logging::log<logging::level::INFO>(
189 "Failed to find metadata",
190 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600191 }
192 }
193
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500194 sd_journal_close(j);
195
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600196 // Create error Entry dbus object
197 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500198 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500199 {
200 infoErrors.push_back(entryId);
201 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600202 else
203 {
204 realErrors.push_back(entryId);
205 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600206 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700207 std::chrono::system_clock::now().time_since_epoch())
208 .count();
209 auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600210
Patrick Venturef18bf832018-10-26 18:14:00 -0700211 AssociationList objects{};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600212 processMetadata(errMsg, additionalData, objects);
213
Patrick Venturef18bf832018-10-26 18:14:00 -0700214 auto e = std::make_unique<Entry>(busLog, objPath, entryId,
215 ms, // Milliseconds since 1970
216 errLvl, std::move(errMsg),
217 std::move(additionalData),
218 std::move(objects), fwVersion, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500219 serialize(*e);
220 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500221}
222
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600223void Manager::processMetadata(const std::string& errorName,
224 const std::vector<std::string>& additionalData,
225 AssociationList& objects) const
226{
227 // additionalData is a list of "metadata=value"
228 constexpr auto separator = '=';
Patrick Venture34438962018-10-30 13:17:37 -0700229 for (const auto& entryItem : additionalData)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600230 {
Patrick Venture34438962018-10-30 13:17:37 -0700231 auto found = entryItem.find(separator);
Patrick Venturef18bf832018-10-26 18:14:00 -0700232 if (std::string::npos != found)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600233 {
Patrick Venture34438962018-10-30 13:17:37 -0700234 auto metadata = entryItem.substr(0, found);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600235 auto iter = meta.find(metadata);
Patrick Venturef18bf832018-10-26 18:14:00 -0700236 if (meta.end() != iter)
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600237 {
238 (iter->second)(metadata, additionalData, objects);
239 }
240 }
241 }
242}
243
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500244void Manager::erase(uint32_t entryId)
245{
Patrick Venture34438962018-10-30 13:17:37 -0700246 auto entryFound = entries.find(entryId);
247 if (entries.end() != entryFound)
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500248 {
Deepak Kodihalli33887992017-06-13 07:06:49 -0500249 // Delete the persistent representation of this error.
250 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600251 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500252 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600253
Patrick Venturef18bf832018-10-26 18:14:00 -0700254 auto removeId = [](std::list<uint32_t>& ids, uint32_t id) {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600255 auto it = std::find(ids.begin(), ids.end(), id);
256 if (it != ids.end())
257 {
258 ids.erase(it);
259 }
260 };
Patrick Venture34438962018-10-30 13:17:37 -0700261 if (entryFound->second->severity() >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500262 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600263 removeId(infoErrors, entryId);
264 }
265 else
266 {
267 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500268 }
Patrick Venture34438962018-10-30 13:17:37 -0700269 entries.erase(entryFound);
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500270 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600271 else
272 {
273 logging::log<level::ERR>("Invalid entry ID to delete",
Patrick Venturef18bf832018-10-26 18:14:00 -0700274 logging::entry("ID=%d", entryId));
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600275 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500276}
277
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500278void Manager::restore()
279{
Patrick Venturef18bf832018-10-26 18:14:00 -0700280 auto sanity = [](const auto& id, const auto& restoredId) {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600281 return id == restoredId;
282 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500283 std::vector<uint32_t> errorIds;
284
285 fs::path dir(ERRLOG_PERSIST_PATH);
286 if (!fs::exists(dir) || fs::is_empty(dir))
287 {
288 return;
289 }
290
Patrick Venturef18bf832018-10-26 18:14:00 -0700291 for (auto& file : fs::directory_iterator(dir))
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500292 {
293 auto id = file.path().filename().c_str();
294 auto idNum = std::stol(id);
295 auto e = std::make_unique<Entry>(
Patrick Venturef18bf832018-10-26 18:14:00 -0700296 busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this);
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500297 if (deserialize(file.path(), *e))
298 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700299 // validate the restored error entry id
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600300 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500301 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600302 e->emit_object_added();
303 if (e->severity() >= Entry::sevLowerLimit)
304 {
305 infoErrors.push_back(idNum);
306 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600307 else
308 {
309 realErrors.push_back(idNum);
310 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600311
312 entries.insert(std::make_pair(idNum, std::move(e)));
313 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500314 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600315 else
316 {
317 logging::log<logging::level::ERR>(
318 "Failed in sanity check while restoring error entry. "
319 "Ignoring error entry",
320 logging::entry("ID_NUM=%d", idNum),
321 logging::entry("ENTRY_ID=%d", e->id()));
322 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500323 }
324 }
325
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530326 if (!errorIds.empty())
327 {
328 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
329 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500330}
331
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500332void Manager::journalSync()
333{
334 bool syncRequested = false;
335 auto fd = -1;
336 auto rc = -1;
337 auto wd = -1;
338 auto bus = sdbusplus::bus::new_default();
339
340 auto start =
341 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
342 .count();
343
344 constexpr auto maxRetry = 2;
345 for (int i = 0; i < maxRetry; i++)
346 {
347 // Read timestamp from synced file
348 constexpr auto syncedPath = "/run/systemd/journal/synced";
349 std::ifstream syncedFile(syncedPath);
350 if (syncedFile.fail())
351 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500352 // If the synced file doesn't exist, a sync request will create it.
353 if (errno != ENOENT)
354 {
355 log<level::ERR>("Failed to open journal synced file",
356 entry("FILENAME=%s", syncedPath),
357 entry("ERRNO=%d", errno));
358 return;
359 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500360 }
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500361 else
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500362 {
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500363 // Only read the synced file if it exists.
364 // See if a sync happened by now
365 std::string timestampStr;
366 std::getline(syncedFile, timestampStr);
Patrick Venture30047bf2018-11-01 18:52:15 -0700367 auto timestamp = std::stoll(timestampStr);
Adriana Kobylak7d111a82018-09-04 10:14:24 -0500368 if (timestamp >= start)
369 {
370 return;
371 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500372 }
373
374 // Let's ask for a sync, but only once
375 if (!syncRequested)
376 {
377 syncRequested = true;
378
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500379 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
380 auto signal = SIGRTMIN + 1;
381
382 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
383 SYSTEMD_INTERFACE, "KillUnit");
384 method.append(JOURNAL_UNIT, "main", signal);
385 bus.call(method);
386 if (method.is_method_error())
387 {
388 log<level::ERR>("Failed to kill journal service");
389 return;
390 }
391 continue;
392 }
393
394 // Let's install the inotify watch, if we didn't do that yet. This watch
395 // monitors the syncedFile for when journald updates it with a newer
396 // timestamp. This means the journal has been flushed.
397 if (fd < 0)
398 {
399 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
400 if (fd < 0)
401 {
402 log<level::ERR>("Failed to create inotify watch",
403 entry("ERRNO=%d", errno));
404 return;
405 }
406
407 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
408 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
409 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
410 if (wd < 0)
411 {
412 log<level::ERR>("Failed to watch journal directory",
413 entry("PATH=%s", JOURNAL_RUN_PATH),
414 entry("ERRNO=%d", errno));
415 close(fd);
416 return;
417 }
418 continue;
419 }
420
421 // Let's wait until inotify reports an event
422 struct pollfd fds = {
423 .fd = fd,
424 .events = POLLIN,
425 };
426 constexpr auto pollTimeout = 5; // 5 seconds
427 rc = poll(&fds, 1, pollTimeout * 1000);
428 if (rc < 0)
429 {
430 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
431 entry("ERR=%s", strerror(-rc)));
432 inotify_rm_watch(fd, wd);
433 close(fd);
434 return;
435 }
436 else if (rc == 0)
437 {
438 log<level::INFO>("Poll timeout, no new journal synced data",
439 entry("TIMEOUT=%d", pollTimeout));
440 break;
441 }
442
443 // Read from the specified file descriptor until there is no new data,
444 // throwing away everything read since the timestamp will be read at the
445 // beginning of the loop.
446 constexpr auto maxBytes = 64;
447 uint8_t buffer[maxBytes];
448 while (read(fd, buffer, maxBytes) > 0)
449 ;
450 }
451
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500452 if (fd != -1)
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500453 {
Adriana Kobylak4a029f22018-05-23 12:58:19 -0500454 if (wd != -1)
455 {
456 inotify_rm_watch(fd, wd);
457 }
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500458 close(fd);
459 }
460
461 return;
462}
463
Matt Spinler1275bd12018-05-01 15:13:53 -0500464std::string Manager::readFWVersion()
465{
466 std::string version;
467 std::ifstream versionFile{BMC_VERSION_FILE};
468 std::string line;
469 static constexpr auto VERSION_ID = "VERSION_ID=";
470
471 while (std::getline(versionFile, line))
472 {
473 if (line.find(VERSION_ID) != std::string::npos)
474 {
475 auto pos = line.find_first_of('"') + 1;
476 version = line.substr(pos, line.find_last_of('"') - pos);
477 break;
478 }
479 }
480
481 if (version.empty())
482 {
483 log<level::ERR>("Unable to read BMC firmware version");
484 }
485
486 return version;
487}
488
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500489} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600490} // namespace logging
Patrick Venturef18bf832018-10-26 18:14:00 -0700491} // namespace phosphor