blob: d587ec4251d8832b6c3aaa80019a1cac19237188 [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{
Adriana Kobylak7298dc22017-01-24 12:21:50 -060029 constexpr const auto transactionIdVar = "TRANSACTION_ID";
Adriana Kobylak27c87d92017-03-06 12:45:09 -060030 // Length of 'TRANSACTION_ID' string.
Adriana Kobylak67218992017-02-28 12:53:37 -060031 constexpr const auto transactionIdVarSize = strlen(transactionIdVar);
Adriana Kobylak27c87d92017-03-06 12:45:09 -060032 // Length of 'TRANSACTION_ID=' string.
33 constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1;
Adriana Kobylak1db1bd32016-10-10 11:39:20 -050034
Adriana Kobylakd311bc82016-10-16 09:54:40 -050035 sd_journal *j = nullptr;
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060036 int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY);
Adriana Kobylakd311bc82016-10-16 09:54:40 -050037 if (rc < 0)
38 {
39 logging::log<logging::level::ERR>("Failed to open journal",
40 logging::entry("DESCRIPTION=%s", strerror(-rc)));
Adriana Kobylak8f7941e2016-11-14 14:46:23 -060041 return;
Adriana Kobylakd311bc82016-10-16 09:54:40 -050042 }
43
Adriana Kobylak7298dc22017-01-24 12:21:50 -060044 std::string transactionIdStr = std::to_string(transactionId);
Adriana Kobylakd722b3a2017-02-28 12:10:44 -060045 std::set<std::string> metalist;
46 auto metamap = g_errMetaMap.find(errMsg);
47 if (metamap != g_errMetaMap.end())
48 {
49 metalist.insert(metamap->second.begin(), metamap->second.end());
50 }
Adriana Kobylak7298dc22017-01-24 12:21:50 -060051
Tom Joseph7a33ee42017-07-25 00:04:20 +053052 std::vector<std::string> additionalData;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060053
Adriana Kobylakfbe88722017-02-22 16:49:59 -060054 // Read the journal from the end to get the most recent entry first.
55 // The result from the sd_journal_get_data() is of the form VARIABLE=value.
56 SD_JOURNAL_FOREACH_BACKWARDS(j)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060057 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -060058 const char *data = nullptr;
59 size_t length = 0;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060060
Adriana Kobylakfbe88722017-02-22 16:49:59 -060061 // Look for the transaction id metadata variable
62 rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data,
63 &length);
64 if (rc < 0)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060065 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -060066 // This journal entry does not have the TRANSACTION_ID
67 // metadata variable.
68 continue;
69 }
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060070
Adriana Kobylak27c87d92017-03-06 12:45:09 -060071 // journald does not guarantee that sd_journal_get_data() returns NULL
72 // terminated strings, so need to specify the size to use to compare,
73 // use the returned length instead of anything that relies on NULL
74 // terminators like strlen().
75 // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove
76 // the TRANSACTION_ID characters plus the (=) sign to do the comparison.
77 // 'data + transactionIdVarOffset' will be in the form of '1234'.
78 // 'length - transactionIdVarOffset' will be the length of '1234'.
79 if ((length <= (transactionIdVarOffset)) ||
80 (transactionIdStr.compare(0,
81 transactionIdStr.size(),
82 data + transactionIdVarOffset,
83 length - transactionIdVarOffset) != 0))
Adriana Kobylakfbe88722017-02-22 16:49:59 -060084 {
85 // The value of the TRANSACTION_ID metadata is not the requested
86 // transaction id number.
87 continue;
88 }
89
90 // Search for all metadata variables in the current journal entry.
91 for (auto i = metalist.cbegin(); i != metalist.cend();)
92 {
93 rc = sd_journal_get_data(j, (*i).c_str(),
94 (const void **)&data, &length);
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060095 if (rc < 0)
96 {
Adriana Kobylakfbe88722017-02-22 16:49:59 -060097 // Metadata variable not found, check next metadata variable.
98 i++;
Adriana Kobylak9aa7d782017-02-18 09:20:49 -060099 continue;
100 }
101
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600102 // Metadata variable found, save it and remove it from the set.
103 additionalData.emplace_back(data, length);
104 i = metalist.erase(i);
105 }
106 if (metalist.empty())
107 {
108 // All metadata variables found, break out of journal loop.
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600109 break;
110 }
Adriana Kobylakfbe88722017-02-22 16:49:59 -0600111 }
112 if (!metalist.empty())
113 {
114 // Not all the metadata variables were found in the journal.
115 for (auto& metaVarStr : metalist)
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600116 {
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600117 logging::log<logging::level::INFO>("Failed to find metadata",
118 logging::entry("META_FIELD=%s", metaVarStr.c_str()));
Adriana Kobylak9aa7d782017-02-18 09:20:49 -0600119 }
120 }
121
Adriana Kobylakd311bc82016-10-16 09:54:40 -0500122 sd_journal_close(j);
123
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600124 // Create error Entry dbus object
125 entryId++;
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600126 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
127 std::chrono::system_clock::now().time_since_epoch()).count();
Adriana Kobylak4ea7f312017-01-10 12:52:34 -0600128 auto objPath = std::string(OBJ_ENTRY) + '/' +
Adriana Kobylakc5f0bbd2017-01-22 14:56:04 -0600129 std::to_string(entryId);
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600130
Deepak Kodihalli35b46372017-02-27 04:58:18 -0600131 AssociationList objects {};
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600132 processMetadata(errMsg, additionalData, objects);
133
Marri Devender Rao0a71bad2017-07-12 08:01:39 -0500134 level reqLevel = level::ERR; // Default to ERR
Adriana Kobylakd722b3a2017-02-28 12:10:44 -0600135 auto levelmap = g_errLevelMap.find(errMsg);
136 if (levelmap != g_errLevelMap.end())
137 {
138 reqLevel = levelmap->second;
139 }
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500140 auto e = std::make_unique<Entry>(
141 busLog,
142 objPath,
143 entryId,
144 ms, // Milliseconds since 1970
145 static_cast<Entry::Level>(reqLevel),
146 std::move(errMsg),
147 std::move(additionalData),
148 std::move(objects),
149 *this);
150 serialize(*e);
151 entries.insert(std::make_pair(entryId, std::move(e)));
Adriana Kobylak1db1bd32016-10-10 11:39:20 -0500152}
153
Deepak Kodihallia87c1572017-02-28 07:40:34 -0600154void Manager::processMetadata(const std::string& errorName,
155 const std::vector<std::string>& additionalData,
156 AssociationList& objects) const
157{
158 // additionalData is a list of "metadata=value"
159 constexpr auto separator = '=';
160 for(const auto& entry: additionalData)
161 {
162 auto found = entry.find(separator);
163 if(std::string::npos != found)
164 {
165 auto metadata = entry.substr(0, found);
166 auto iter = meta.find(metadata);
167 if(meta.end() != iter)
168 {
169 (iter->second)(metadata, additionalData, objects);
170 }
171 }
172 }
173}
174
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500175void Manager::erase(uint32_t entryId)
176{
177 auto entry = entries.find(entryId);
Deepak Kodihalli33887992017-06-13 07:06:49 -0500178 auto id = entry->second->id();
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500179 if(entries.end() != entry)
180 {
Deepak Kodihalli33887992017-06-13 07:06:49 -0500181 // Delete the persistent representation of this error.
182 fs::path errorPath(ERRLOG_PERSIST_PATH);
183 errorPath /= std::to_string(id);
184 fs::remove(errorPath);
185
Deepak Kodihalli99a85492017-03-31 06:01:57 -0500186 entries.erase(entry);
187 }
188}
189
Deepak Kodihalli72654f12017-06-12 04:33:29 -0500190void Manager::restore()
191{
192 std::vector<uint32_t> errorIds;
193
194 fs::path dir(ERRLOG_PERSIST_PATH);
195 if (!fs::exists(dir) || fs::is_empty(dir))
196 {
197 return;
198 }
199
200 for(auto& file: fs::directory_iterator(dir))
201 {
202 auto id = file.path().filename().c_str();
203 auto idNum = std::stol(id);
204 auto e = std::make_unique<Entry>(
205 busLog,
206 std::string(OBJ_ENTRY) + '/' + id,
207 idNum,
208 *this);
209 if (deserialize(file.path(), *e))
210 {
211 e->emit_object_added();
212 entries.insert(std::make_pair(idNum, std::move(e)));
213 errorIds.push_back(idNum);
214 }
215 }
216
217 entryId = *(std::max_element(errorIds.begin(), errorIds.end()));
218}
219
Adriana Kobylak8f7941e2016-11-14 14:46:23 -0600220} // namespace logging
221} // namepsace phosphor