blob: a21b0fa086d2008e91559d4d4815d50e26def719 [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
2 * Copyright © 2019 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Matt Spinler89fa0822019-07-17 13:54:30 -050016#include "repository.hpp"
17
18#include <fstream>
19#include <phosphor-logging/log.hpp>
20#include <xyz/openbmc_project/Common/File/error.hpp>
21
22namespace openpower
23{
24namespace pels
25{
26
27namespace fs = std::filesystem;
28using namespace phosphor::logging;
29namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
30
31Repository::Repository(const std::filesystem::path& basePath) :
32 _logPath(basePath / "logs")
33{
34 if (!fs::exists(_logPath))
35 {
36 fs::create_directories(_logPath);
37 }
Matt Spinler475e5742019-07-18 16:09:49 -050038
39 restore();
40}
41
42void Repository::restore()
43{
44 for (auto& dirEntry : fs::directory_iterator(_logPath))
45 {
46 try
47 {
48 if (!fs::is_regular_file(dirEntry.path()))
49 {
50 continue;
51 }
52
53 std::ifstream file{dirEntry.path()};
54 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
55 std::istreambuf_iterator<char>()};
56 file.close();
57
Matt Spinler07eefc52019-09-26 11:18:26 -050058 PEL pel{data};
Matt Spinler475e5742019-07-18 16:09:49 -050059 if (pel.valid())
60 {
Matt Spinler346f99a2019-11-21 13:06:35 -060061 PELAttributes attributes{
62 dirEntry.path(), pel.userHeader().actionFlags(),
63 pel.hostTransmissionState(), pel.hmcTransmissionState()};
Matt Spinler0ff00482019-11-06 16:19:46 -060064
Matt Spinler475e5742019-07-18 16:09:49 -050065 using pelID = LogID::Pel;
66 using obmcID = LogID::Obmc;
Matt Spinler0ff00482019-11-06 16:19:46 -060067 _pelAttributes.emplace(
Matt Spinler475e5742019-07-18 16:09:49 -050068 LogID(pelID(pel.id()), obmcID(pel.obmcLogID())),
Matt Spinler0ff00482019-11-06 16:19:46 -060069 attributes);
Matt Spinler475e5742019-07-18 16:09:49 -050070 }
71 else
72 {
73 log<level::ERR>(
74 "Found invalid PEL file while restoring. Removing.",
75 entry("FILENAME=%s", dirEntry.path().c_str()));
76 fs::remove(dirEntry.path());
77 }
78 }
79 catch (std::exception& e)
80 {
81 log<level::ERR>("Hit exception while restoring PEL File",
82 entry("FILENAME=%s", dirEntry.path().c_str()),
83 entry("ERROR=%s", e.what()));
84 }
85 }
Matt Spinler89fa0822019-07-17 13:54:30 -050086}
87
88std::string Repository::getPELFilename(uint32_t pelID, const BCDTime& time)
89{
90 char name[50];
91 sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", time.yearMSB,
92 time.yearLSB, time.month, time.day, time.hour, time.minutes,
93 time.seconds, time.hundredths, pelID);
94 return std::string{name};
95}
96
97void Repository::add(std::unique_ptr<PEL>& pel)
98{
99 auto path = _logPath / getPELFilename(pel->id(), pel->commitTime());
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600100
101 write(*(pel.get()), path);
102
Matt Spinler346f99a2019-11-21 13:06:35 -0600103 PELAttributes attributes{path, pel->userHeader().actionFlags(),
104 pel->hostTransmissionState(),
105 pel->hmcTransmissionState()};
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600106
107 using pelID = LogID::Pel;
108 using obmcID = LogID::Obmc;
109 _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())),
110 attributes);
111
112 processAddCallbacks(*pel);
113}
114
115void Repository::write(const PEL& pel, const fs::path& path)
116{
Matt Spinler89fa0822019-07-17 13:54:30 -0500117 std::ofstream file{path, std::ios::binary};
118
119 if (!file.good())
120 {
121 // If this fails, the filesystem is probably full so it isn't like
122 // we could successfully create yet another error log here.
123 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500124 fs::remove(path);
125 log<level::ERR>("Unable to open PEL file for writing",
126 entry("ERRNO=%d", e), entry("PATH=%s", path.c_str()));
127 throw file_error::Open();
128 }
129
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600130 auto data = pel.data();
Matt Spinler89fa0822019-07-17 13:54:30 -0500131 file.write(reinterpret_cast<const char*>(data.data()), data.size());
132
133 if (file.fail())
134 {
135 // Same note as above about not being able to create an error log
136 // for this case even if we wanted.
137 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500138 file.close();
139 fs::remove(path);
140 log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e),
141 entry("PATH=%s", path.c_str()));
142 throw file_error::Write();
143 }
Matt Spinler475e5742019-07-18 16:09:49 -0500144}
145
146void Repository::remove(const LogID& id)
147{
148 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600149 if (pel != _pelAttributes.end())
Matt Spinler475e5742019-07-18 16:09:49 -0500150 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600151 fs::remove(pel->second.path);
152 _pelAttributes.erase(pel);
Matt Spinler421f6532019-11-06 15:40:45 -0600153
154 processDeleteCallbacks(id.pelID.id);
Matt Spinler475e5742019-07-18 16:09:49 -0500155 }
Matt Spinler89fa0822019-07-17 13:54:30 -0500156}
157
Matt Spinler2813f362019-07-19 12:45:28 -0500158std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id)
159{
160 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600161 if (pel != _pelAttributes.end())
Matt Spinler2813f362019-07-19 12:45:28 -0500162 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600163 std::ifstream file{pel->second.path.c_str()};
Matt Spinler2813f362019-07-19 12:45:28 -0500164 if (!file.good())
165 {
166 auto e = errno;
167 log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600168 entry("PATH=%s", pel->second.path.c_str()));
Matt Spinler2813f362019-07-19 12:45:28 -0500169 throw file_error::Open();
170 }
171
172 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
173 std::istreambuf_iterator<char>()};
174 return data;
175 }
176
177 return std::nullopt;
178}
179
Matt Spinler1ea78802019-11-01 13:04:59 -0500180void Repository::for_each(ForEachFunc func) const
181{
Matt Spinler0ff00482019-11-06 16:19:46 -0600182 for (const auto& [id, attributes] : _pelAttributes)
Matt Spinler1ea78802019-11-01 13:04:59 -0500183 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600184 std::ifstream file{attributes.path};
Matt Spinler1ea78802019-11-01 13:04:59 -0500185
186 if (!file.good())
187 {
188 auto e = errno;
189 log<level::ERR>("Repository::for_each: Unable to open PEL file",
190 entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600191 entry("PATH=%s", attributes.path.c_str()));
Matt Spinler1ea78802019-11-01 13:04:59 -0500192 continue;
193 }
194
195 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
196 std::istreambuf_iterator<char>()};
197 file.close();
198
199 PEL pel{data};
200
201 try
202 {
203 if (func(pel))
204 {
205 break;
206 }
207 }
208 catch (std::exception& e)
209 {
210 log<level::ERR>("Repository::for_each function exception",
211 entry("ERROR=%s", e.what()));
212 }
213 }
214}
215
Matt Spinler421f6532019-11-06 15:40:45 -0600216void Repository::processAddCallbacks(const PEL& pel) const
217{
218 for (auto& [name, func] : _addSubscriptions)
219 {
220 try
221 {
222 func(pel);
223 }
224 catch (std::exception& e)
225 {
226 log<level::ERR>("PEL Repository add callback exception",
227 entry("NAME=%s", name.c_str()),
228 entry("ERROR=%s", e.what()));
229 }
230 }
231}
232
233void Repository::processDeleteCallbacks(uint32_t id) const
234{
235 for (auto& [name, func] : _deleteSubscriptions)
236 {
237 try
238 {
239 func(id);
240 }
241 catch (std::exception& e)
242 {
243 log<level::ERR>("PEL Repository delete callback exception",
244 entry("NAME=%s", name.c_str()),
245 entry("ERROR=%s", e.what()));
246 }
247 }
248}
249
Matt Spinler0ff00482019-11-06 16:19:46 -0600250std::optional<std::reference_wrapper<const Repository::PELAttributes>>
251 Repository::getPELAttributes(const LogID& id) const
252{
253 auto pel = findPEL(id);
254 if (pel != _pelAttributes.end())
255 {
256 return pel->second;
257 }
258
259 return std::nullopt;
260}
261
Matt Spinler89fa0822019-07-17 13:54:30 -0500262} // namespace pels
263} // namespace openpower