blob: 641ed7bcd85679b9a37e554affd4f72eefe0bfab [file] [log] [blame]
Adriana Kobylakd311bc82016-10-16 09:54:40 -05001#include <fstream>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -05002#include <future>
Adriana Kobylakd311bc82016-10-16 09:54:40 -05003#include <iostream>
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -06004#include <chrono>
Adriana Kobylakd311bc82016-10-16 09:54:40 -05005#include <cstdio>
Adriana Kobylak5f4247f2018-03-15 10:27:05 -05006#include <poll.h>
Adriana Kobylakfbe88722017-02-22 16:49:59 -06007#include <set>
Adriana Kobylakd311bc82016-10-16 09:54:40 -05008#include <string>
9#include <vector>
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050010#include <sdbusplus/vtable.hpp>
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>
Adriana Kobylak4ea7f312017-01-10 12:52:34 -060015#include "config.h"
16#include "elog_entry.hpp"
Saqib Khan2bb15192017-02-13 13:19:55 -060017#include <phosphor-logging/log.hpp>
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060018#include "log_manager.hpp"
Deepak Kodihallia87c1572017-02-28 07:40:34 -060019#include "elog_meta.hpp"
Deepak Kodihalli72654f12017-06-12 04:33:29 -050020#include "elog_serialize.hpp"
Deepak Kodihallia87c1572017-02-28 07:40:34 -060021
22using namespace phosphor::logging;
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050023using namespace std::chrono;
Deepak Kodihallia87c1572017-02-28 07:40:34 -060024extern const std::map<metadata::Metadata,
25 std::function<metadata::associations::Type>> meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050026
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060027namespace phosphor
28{
29namespace logging
30{
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -050031namespace internal
32{
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050033inline auto getLevel(const std::string& errMsg)
34{
35 auto reqLevel = Entry::Level::Error; // Default to Error
36
37 auto levelmap = g_errLevelMap.find(errMsg);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050038 if (levelmap != g_errLevelMap.end())
Marri Devender Rao7656fba2017-08-06 05:42:52 -050039 {
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050040 reqLevel = static_cast<Entry::Level>(levelmap->second);
Marri Devender Rao7656fba2017-08-06 05:42:52 -050041 }
42
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -050043 return reqLevel;
44}
45
46void Manager::commit(uint64_t transactionId, std::string errMsg)
47{
48 auto level = getLevel(errMsg);
49 _commit(transactionId, std::move(errMsg), level);
50}
51
52void Manager::commitWithLvl(uint64_t transactionId, std::string errMsg,
53 uint32_t errLvl)
54{
55 _commit(transactionId, std::move(errMsg),
56 static_cast<Entry::Level>(errLvl));
57}
58
59void Manager::_commit(uint64_t transactionId, std::string&& errMsg,
60 Entry::Level errLvl)
61{
62 if (errLvl < Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050063 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -060064 if (realErrors.size() >= ERROR_CAP)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050065 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -060066 erase(realErrors.front());
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -050067 }
68 }
69 else
70 {
71 if (infoErrors.size() >= ERROR_INFO_CAP)
72 {
73 erase(infoErrors.front());
74 }
75 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -060076 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -060077 // Length of 'TRANSACTION_ID' string.
Adriana Kobylak67218992017-02-28 12:53:37 -060078 constexpr const auto transactionIdVarSize = strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -060079 // Length of 'TRANSACTION_ID=' string.
80 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050081
Adriana Kobylak5f4247f2018-03-15 10:27:05 -050082 // Flush all the pending log messages into the journal
83 journalSync();
Adriana Kobylakcfd9a7d2017-06-07 11:57:31 -050084
Adriana Kobylakd311bc82016-10-16 09:54:40 -050085 sd_journal *j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060086 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050087 if (rc < 0)
88 {
89 logging::log<logging::level::ERR>("Failed to open journal",
90 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060091 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -050092 }
93
Adriana Kobylak7298dc22017-01-24 12:21:50 -060094 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -060095 std::set<std::string> metalist;
96 auto metamap = g_errMetaMap.find(errMsg);
97 if (metamap != g_errMetaMap.end())
98 {
99 metalist.insert(metamap->second.begin(), metamap->second.end());
100 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -0600101
Jayanth Othayothdb18ebe2017-09-04 00:48:02 -0500102 //Add _PID field information in AdditionalData.
103 metalist.insert("_PID");
104
Tom Joseph7a33ee42017-07-25 00:04:20 +0530105 std::vector<std::string> additionalData;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600106
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600107 // Read the journal from the end to get the most recent entry first.
108 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
109 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600110 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600111 const char *data = nullptr;
112 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600113
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600114 // Look for the transaction id metadata variable
115 rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data,
116 &length);
117 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600118 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600119 // This journal entry does not have the TRANSACTION_ID
120 // metadata variable.
121 continue;
122 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600123
Adriana Kobylak27c87d92017-03-06 12:45:09 -0600124 // journald does not guarantee that sd_journal_get_data() returns NULL
125 // terminated strings, so need to specify the size to use to compare,
126 // use the returned length instead of anything that relies on NULL
127 // terminators like strlen().
128 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
129 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
130 // 'data + transactionIdVarOffset' will be in the form of '1234'.
131 // 'length - transactionIdVarOffset' will be the length of '1234'.
132 if ((length <= (transactionIdVarOffset)) ||
133 (transactionIdStr.compare(0,
134 transactionIdStr.size(),
135 data + transactionIdVarOffset,
136 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600137 {
138 // The value of the TRANSACTION_ID metadata is not the requested
139 // transaction id number.
140 continue;
141 }
142
143 // Search for all metadata variables in the current journal entry.
144 for (auto i = metalist.cbegin(); i != metalist.cend();)
145 {
146 rc = sd_journal_get_data(j, (*i).c_str(),
147 (const void **)&data, &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600148 if (rc < 0)
149 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600150 // Metadata variable not found, check next metadata variable.
151 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600152 continue;
153 }
154
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600155 // Metadata variable found, save it and remove it from the set.
156 additionalData.emplace_back(data, length);
157 i = metalist.erase(i);
158 }
159 if (metalist.empty())
160 {
161 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600162 break;
163 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600164 }
165 if (!metalist.empty())
166 {
167 // Not all the metadata variables were found in the journal.
168 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600169 {
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600170 logging::log<logging::level::INFO>("Failed to find metadata",
171 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600172 }
173 }
174
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500175 sd_journal_close(j);
176
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600177 // Create error Entry dbus object
178 entryId++;
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500179 if (errLvl >= Entry::sevLowerLimit)
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500180 {
181 infoErrors.push_back(entryId);
182 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600183 else
184 {
185 realErrors.push_back(entryId);
186 }
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600187 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
188 std::chrono::system_clock::now().time_since_epoch()).count();
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600189 auto objPath = std::string(OBJ_ENTRY) + '/' +
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600190 std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600191
Deepak Kodihalli35b46372017-02-27 04:58:18 -0600192 AssociationList objects {};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600193 processMetadata(errMsg, additionalData, objects);
194
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500195 auto e = std::make_unique<Entry>(
196 busLog,
197 objPath,
198 entryId,
199 ms, // Milliseconds since 1970
Deepak Kodihalli6fd9dc42018-04-03 02:08:42 -0500200 errLvl,
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500201 std::move(errMsg),
202 std::move(additionalData),
203 std::move(objects),
204 *this);
205 serialize(*e);
206 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500207}
208
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600209void Manager::processMetadata(const std::string& errorName,
210 const std::vector<std::string>& additionalData,
211 AssociationList& objects) const
212{
213 // additionalData is a list of "metadata=value"
214 constexpr auto separator = '=';
215 for(const auto& entry: additionalData)
216 {
217 auto found = entry.find(separator);
218 if(std::string::npos != found)
219 {
220 auto metadata = entry.substr(0, found);
221 auto iter = meta.find(metadata);
222 if(meta.end() != iter)
223 {
224 (iter->second)(metadata, additionalData, objects);
225 }
226 }
227 }
228}
229
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500230void Manager::erase(uint32_t entryId)
231{
232 auto entry = entries.find(entryId);
233 if(entries.end() != entry)
234 {
Deepak Kodihalli33887992017-06-13 07:06:49 -0500235 // Delete the persistent representation of this error.
236 fs::path errorPath(ERRLOG_PERSIST_PATH);
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600237 errorPath /= std::to_string(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500238 fs::remove(errorPath);
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600239
240 auto removeId = [](std::list<uint32_t>& ids , uint32_t id)
241 {
242 auto it = std::find(ids.begin(), ids.end(), id);
243 if (it != ids.end())
244 {
245 ids.erase(it);
246 }
247 };
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500248 if (entry->second->severity() >= Entry::sevLowerLimit)
249 {
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600250 removeId(infoErrors, entryId);
251 }
252 else
253 {
254 removeId(realErrors, entryId);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500255 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500256 entries.erase(entry);
257 }
Marri Devender Rao8959efc2017-11-17 00:13:41 -0600258 else
259 {
260 logging::log<level::ERR>("Invalid entry ID to delete",
261 logging::entry("ID=%d", entryId));
262 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500263}
264
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500265void Manager::restore()
266{
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600267 auto sanity = [](const auto& id, const auto& restoredId)
268 {
269 return id == restoredId;
270 };
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500271 std::vector<uint32_t> errorIds;
272
273 fs::path dir(ERRLOG_PERSIST_PATH);
274 if (!fs::exists(dir) || fs::is_empty(dir))
275 {
276 return;
277 }
278
279 for(auto& file: fs::directory_iterator(dir))
280 {
281 auto id = file.path().filename().c_str();
282 auto idNum = std::stol(id);
283 auto e = std::make_unique<Entry>(
284 busLog,
285 std::string(OBJ_ENTRY) + '/' + id,
286 idNum,
287 *this);
288 if (deserialize(file.path(), *e))
289 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600290 //validate the restored error entry id
291 if (sanity(static_cast<uint32_t>(idNum), e->id()))
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500292 {
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600293 e->emit_object_added();
294 if (e->severity() >= Entry::sevLowerLimit)
295 {
296 infoErrors.push_back(idNum);
297 }
Nagaraju Gorugantie4b0b772017-11-30 02:12:45 -0600298 else
299 {
300 realErrors.push_back(idNum);
301 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600302
303 entries.insert(std::make_pair(idNum, std::move(e)));
304 errorIds.push_back(idNum);
Nagaraju Gorugantif8a5a792017-10-13 08:09:52 -0500305 }
Marri Devender Rao979ed1c2017-11-17 05:06:25 -0600306 else
307 {
308 logging::log<logging::level::ERR>(
309 "Failed in sanity check while restoring error entry. "
310 "Ignoring error entry",
311 logging::entry("ID_NUM=%d", idNum),
312 logging::entry("ENTRY_ID=%d", e->id()));
313 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500314 }
315 }
316
Vishwanatha Subbanna37af9ba2017-09-28 16:33:53 +0530317 if (!errorIds.empty())
318 {
319 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
320 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500321}
322
Adriana Kobylak5f4247f2018-03-15 10:27:05 -0500323void Manager::journalSync()
324{
325 bool syncRequested = false;
326 auto fd = -1;
327 auto rc = -1;
328 auto wd = -1;
329 auto bus = sdbusplus::bus::new_default();
330
331 auto start =
332 duration_cast<microseconds>(steady_clock::now().time_since_epoch())
333 .count();
334
335 constexpr auto maxRetry = 2;
336 for (int i = 0; i < maxRetry; i++)
337 {
338 // Read timestamp from synced file
339 constexpr auto syncedPath = "/run/systemd/journal/synced";
340 std::ifstream syncedFile(syncedPath);
341 if (syncedFile.fail())
342 {
343 log<level::ERR>("Failed to open journal synced file",
344 entry("FILENAME=%s", syncedPath),
345 entry("ERRNO=%d", errno));
346 return;
347 }
348
349 // See if a sync happened by now
350 std::string timestampStr;
351 std::getline(syncedFile, timestampStr);
352 auto timestamp = stoll(timestampStr);
353 if (timestamp >= start)
354 {
355 return;
356 }
357
358 // Let's ask for a sync, but only once
359 if (!syncRequested)
360 {
361 syncRequested = true;
362
363 constexpr auto SYSTEMD_BUSNAME = "org.freedesktop.systemd1";
364 constexpr auto SYSTEMD_PATH = "/org/freedesktop/systemd1";
365 constexpr auto SYSTEMD_INTERFACE =
366 "org.freedesktop.systemd1.Manager";
367 constexpr auto JOURNAL_UNIT = "systemd-journald.service";
368 auto signal = SIGRTMIN + 1;
369
370 auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH,
371 SYSTEMD_INTERFACE, "KillUnit");
372 method.append(JOURNAL_UNIT, "main", signal);
373 bus.call(method);
374 if (method.is_method_error())
375 {
376 log<level::ERR>("Failed to kill journal service");
377 return;
378 }
379 continue;
380 }
381
382 // Let's install the inotify watch, if we didn't do that yet. This watch
383 // monitors the syncedFile for when journald updates it with a newer
384 // timestamp. This means the journal has been flushed.
385 if (fd < 0)
386 {
387 fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
388 if (fd < 0)
389 {
390 log<level::ERR>("Failed to create inotify watch",
391 entry("ERRNO=%d", errno));
392 return;
393 }
394
395 constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal";
396 wd = inotify_add_watch(fd, JOURNAL_RUN_PATH,
397 IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR);
398 if (wd < 0)
399 {
400 log<level::ERR>("Failed to watch journal directory",
401 entry("PATH=%s", JOURNAL_RUN_PATH),
402 entry("ERRNO=%d", errno));
403 close(fd);
404 return;
405 }
406 continue;
407 }
408
409 // Let's wait until inotify reports an event
410 struct pollfd fds = {
411 .fd = fd,
412 .events = POLLIN,
413 };
414 constexpr auto pollTimeout = 5; // 5 seconds
415 rc = poll(&fds, 1, pollTimeout * 1000);
416 if (rc < 0)
417 {
418 log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno),
419 entry("ERR=%s", strerror(-rc)));
420 inotify_rm_watch(fd, wd);
421 close(fd);
422 return;
423 }
424 else if (rc == 0)
425 {
426 log<level::INFO>("Poll timeout, no new journal synced data",
427 entry("TIMEOUT=%d", pollTimeout));
428 break;
429 }
430
431 // Read from the specified file descriptor until there is no new data,
432 // throwing away everything read since the timestamp will be read at the
433 // beginning of the loop.
434 constexpr auto maxBytes = 64;
435 uint8_t buffer[maxBytes];
436 while (read(fd, buffer, maxBytes) > 0)
437 ;
438 }
439
440 if ((fd != -1) && (wd != -1))
441 {
442 inotify_rm_watch(fd, wd);
443 close(fd);
444 }
445
446 return;
447}
448
Matt Spinler1275bd12018-05-01 15:13:53 -0500449std::string Manager::readFWVersion()
450{
451 std::string version;
452 std::ifstream versionFile{BMC_VERSION_FILE};
453 std::string line;
454 static constexpr auto VERSION_ID = "VERSION_ID=";
455
456 while (std::getline(versionFile, line))
457 {
458 if (line.find(VERSION_ID) != std::string::npos)
459 {
460 auto pos = line.find_first_of('"') + 1;
461 version = line.substr(pos, line.find_last_of('"') - pos);
462 break;
463 }
464 }
465
466 if (version.empty())
467 {
468 log<level::ERR>("Unable to read BMC firmware version");
469 }
470
471 return version;
472}
473
Nagaraju Goruganti05aae8b2017-08-30 07:56:12 -0500474} // namespace internal
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600475} // namespace logging
476} // namepsace phosphor