blob: 29c0ffe89a961996cfdd3def7dee2be5c263c130 [file] [log] [blame]
Adriana Kobylakd311bc82016-10-16 09:54:40 -05001#include <fstream>
2#include <iostream>
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -06003#include <chrono>
Adriana Kobylakd311bc82016-10-16 09:54:40 -05004#include <cstdio>
Adriana Kobylakfbe88722017-02-22 16:49:59 -06005#include <set>
Adriana Kobylakd311bc82016-10-16 09:54:40 -05006#include <string>
7#include <vector>
Adriana Kobylak1db1bd32016-10-10 11:39:20 -05008#include <sdbusplus/vtable.hpp>
9#include <systemd/sd-bus.h>
Adriana Kobylakd311bc82016-10-16 09:54:40 -050010#include <systemd/sd-journal.h>
Adriana Kobylak4ea7f312017-01-10 12:52:34 -060011#include "config.h"
12#include "elog_entry.hpp"
Saqib Khan2bb15192017-02-13 13:19:55 -060013#include <phosphor-logging/log.hpp>
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060014#include "log_manager.hpp"
Deepak Kodihallia87c1572017-02-28 07:40:34 -060015#include "elog_meta.hpp"
Deepak Kodihalli72654f12017-06-12 04:33:29 -050016#include "elog_serialize.hpp"
Deepak Kodihallia87c1572017-02-28 07:40:34 -060017
18using namespace phosphor::logging;
19extern const std::map<metadata::Metadata,
20 std::function<metadata::associations::Type>> meta;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050021
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060022namespace phosphor
23{
24namespace logging
25{
Adriana Kobylakd311bc82016-10-16 09:54:40 -050026
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060027void Manager::commit(uint64_t transactionId, std::string errMsg)
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050028{
Marri Devender Rao7656fba2017-08-06 05:42:52 -050029 if (capped)
30 {
31 return;
32 }
33 if (entries.size() >= ERROR_CAP)
34 {
35 log<level::ERR>("Reached error cap, Ignoring error",
36 entry("SIZE=%d", entries.size()),
37 entry("ERROR_CAP=%d", ERROR_CAP));
38 capped = true;
39 return;
40 }
41
Adriana Kobylak7298dc22017-01-24 12:21:50 -060042 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -060043 // Length of 'TRANSACTION_ID' string.
Adriana Kobylak67218992017-02-28 12:53:37 -060044 constexpr const auto transactionIdVarSize = strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -060045 // Length of 'TRANSACTION_ID=' string.
46 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050047
Adriana Kobylakcfd9a7d2017-06-07 11:57:31 -050048 // Flush all the pending log messages into the journal via Synchronize
49 constexpr auto JOURNAL_BUSNAME = "org.freedesktop.journal1";
50 constexpr auto JOURNAL_PATH = "/org/freedesktop/journal1";
51 constexpr auto JOURNAL_INTERFACE = "org.freedesktop.journal1";
52 auto bus = sdbusplus::bus::new_default();
53 auto method = bus.new_method_call(JOURNAL_BUSNAME, JOURNAL_PATH,
54 JOURNAL_INTERFACE, "Synchronize");
55 bus.call_noreply(method);
56
Adriana Kobylakd311bc82016-10-16 09:54:40 -050057 sd_journal *j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060058 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050059 if (rc < 0)
60 {
61 logging::log<logging::level::ERR>("Failed to open journal",
62 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060063 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -050064 }
65
Adriana Kobylak7298dc22017-01-24 12:21:50 -060066 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -060067 std::set<std::string> metalist;
68 auto metamap = g_errMetaMap.find(errMsg);
69 if (metamap != g_errMetaMap.end())
70 {
71 metalist.insert(metamap->second.begin(), metamap->second.end());
72 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -060073
Tom Joseph7a33ee42017-07-25 00:04:20 +053074 std::vector<std::string> additionalData;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060075
Adriana Kobylakfbe88722017-02-22 16:49:59 -060076 // Read the journal from the end to get the most recent entry first.
77 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
78 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060079 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -060080 const char *data = nullptr;
81 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060082
Adriana Kobylakfbe88722017-02-22 16:49:59 -060083 // Look for the transaction id metadata variable
84 rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data,
85 &length);
86 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060087 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -060088 // This journal entry does not have the TRANSACTION_ID
89 // metadata variable.
90 continue;
91 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060092
Adriana Kobylak27c87d92017-03-06 12:45:09 -060093 // journald does not guarantee that sd_journal_get_data() returns NULL
94 // terminated strings, so need to specify the size to use to compare,
95 // use the returned length instead of anything that relies on NULL
96 // terminators like strlen().
97 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
98 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
99 // 'data + transactionIdVarOffset' will be in the form of '1234'.
100 // 'length - transactionIdVarOffset' will be the length of '1234'.
101 if ((length <= (transactionIdVarOffset)) ||
102 (transactionIdStr.compare(0,
103 transactionIdStr.size(),
104 data + transactionIdVarOffset,
105 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600106 {
107 // The value of the TRANSACTION_ID metadata is not the requested
108 // transaction id number.
109 continue;
110 }
111
112 // Search for all metadata variables in the current journal entry.
113 for (auto i = metalist.cbegin(); i != metalist.cend();)
114 {
115 rc = sd_journal_get_data(j, (*i).c_str(),
116 (const void **)&data, &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600117 if (rc < 0)
118 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600119 // Metadata variable not found, check next metadata variable.
120 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600121 continue;
122 }
123
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600124 // Metadata variable found, save it and remove it from the set.
125 additionalData.emplace_back(data, length);
126 i = metalist.erase(i);
127 }
128 if (metalist.empty())
129 {
130 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600131 break;
132 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600133 }
134 if (!metalist.empty())
135 {
136 // Not all the metadata variables were found in the journal.
137 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600138 {
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600139 logging::log<logging::level::INFO>("Failed to find metadata",
140 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600141 }
142 }
143
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500144 sd_journal_close(j);
145
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600146 // Create error Entry dbus object
147 entryId++;
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600148 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
149 std::chrono::system_clock::now().time_since_epoch()).count();
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600150 auto objPath = std::string(OBJ_ENTRY) + '/' +
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600151 std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600152
Deepak Kodihalli35b46372017-02-27 04:58:18 -0600153 AssociationList objects {};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600154 processMetadata(errMsg, additionalData, objects);
155
Marri Devender Rao0a71bad2017-07-12 08:01:39 -0500156 level reqLevel = level::ERR; // Default to ERR
Adriana Kobylakd722b3a2017-02-28 12:10:44 -0600157 auto levelmap = g_errLevelMap.find(errMsg);
158 if (levelmap != g_errLevelMap.end())
159 {
160 reqLevel = levelmap->second;
161 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500162 auto e = std::make_unique<Entry>(
163 busLog,
164 objPath,
165 entryId,
166 ms, // Milliseconds since 1970
167 static_cast<Entry::Level>(reqLevel),
168 std::move(errMsg),
169 std::move(additionalData),
170 std::move(objects),
171 *this);
172 serialize(*e);
173 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500174}
175
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600176void Manager::processMetadata(const std::string& errorName,
177 const std::vector<std::string>& additionalData,
178 AssociationList& objects) const
179{
180 // additionalData is a list of "metadata=value"
181 constexpr auto separator = '=';
182 for(const auto& entry: additionalData)
183 {
184 auto found = entry.find(separator);
185 if(std::string::npos != found)
186 {
187 auto metadata = entry.substr(0, found);
188 auto iter = meta.find(metadata);
189 if(meta.end() != iter)
190 {
191 (iter->second)(metadata, additionalData, objects);
192 }
193 }
194 }
195}
196
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500197void Manager::erase(uint32_t entryId)
198{
199 auto entry = entries.find(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500200 auto id = entry->second->id();
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500201 if(entries.end() != entry)
202 {
Deepak Kodihalli33887992017-06-13 07:06:49 -0500203 // Delete the persistent representation of this error.
204 fs::path errorPath(ERRLOG_PERSIST_PATH);
205 errorPath /= std::to_string(id);
206 fs::remove(errorPath);
207
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500208 entries.erase(entry);
209 }
Marri Devender Rao7656fba2017-08-06 05:42:52 -0500210
211 if (entries.size() < ERROR_CAP)
212 {
213 capped = false;
214 }
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500215}
216
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500217void Manager::restore()
218{
219 std::vector<uint32_t> errorIds;
220
221 fs::path dir(ERRLOG_PERSIST_PATH);
222 if (!fs::exists(dir) || fs::is_empty(dir))
223 {
224 return;
225 }
226
227 for(auto& file: fs::directory_iterator(dir))
228 {
229 auto id = file.path().filename().c_str();
230 auto idNum = std::stol(id);
231 auto e = std::make_unique<Entry>(
232 busLog,
233 std::string(OBJ_ENTRY) + '/' + id,
234 idNum,
235 *this);
236 if (deserialize(file.path(), *e))
237 {
238 e->emit_object_added();
239 entries.insert(std::make_pair(idNum, std::move(e)));
240 errorIds.push_back(idNum);
241 }
242 }
243
244 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
245}
246
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600247} // namespace logging
248} // namepsace phosphor