blob: 541445889dfcce6153ca64bc15c6ae793cb11949 [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{
Matt Spinlerdf43a302019-11-21 13:16:56 -060099 pel->setHostTransmissionState(TransmissionState::newPEL);
100 pel->setHMCTransmissionState(TransmissionState::newPEL);
101
Matt Spinler89fa0822019-07-17 13:54:30 -0500102 auto path = _logPath / getPELFilename(pel->id(), pel->commitTime());
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600103
104 write(*(pel.get()), path);
105
Matt Spinler346f99a2019-11-21 13:06:35 -0600106 PELAttributes attributes{path, pel->userHeader().actionFlags(),
107 pel->hostTransmissionState(),
108 pel->hmcTransmissionState()};
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600109
110 using pelID = LogID::Pel;
111 using obmcID = LogID::Obmc;
112 _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())),
113 attributes);
114
115 processAddCallbacks(*pel);
116}
117
118void Repository::write(const PEL& pel, const fs::path& path)
119{
Matt Spinler89fa0822019-07-17 13:54:30 -0500120 std::ofstream file{path, std::ios::binary};
121
122 if (!file.good())
123 {
124 // If this fails, the filesystem is probably full so it isn't like
125 // we could successfully create yet another error log here.
126 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500127 fs::remove(path);
128 log<level::ERR>("Unable to open PEL file for writing",
129 entry("ERRNO=%d", e), entry("PATH=%s", path.c_str()));
130 throw file_error::Open();
131 }
132
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600133 auto data = pel.data();
Matt Spinler89fa0822019-07-17 13:54:30 -0500134 file.write(reinterpret_cast<const char*>(data.data()), data.size());
135
136 if (file.fail())
137 {
138 // Same note as above about not being able to create an error log
139 // for this case even if we wanted.
140 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500141 file.close();
142 fs::remove(path);
143 log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e),
144 entry("PATH=%s", path.c_str()));
145 throw file_error::Write();
146 }
Matt Spinler475e5742019-07-18 16:09:49 -0500147}
148
149void Repository::remove(const LogID& id)
150{
151 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600152 if (pel != _pelAttributes.end())
Matt Spinler475e5742019-07-18 16:09:49 -0500153 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600154 fs::remove(pel->second.path);
155 _pelAttributes.erase(pel);
Matt Spinler421f6532019-11-06 15:40:45 -0600156
157 processDeleteCallbacks(id.pelID.id);
Matt Spinler475e5742019-07-18 16:09:49 -0500158 }
Matt Spinler89fa0822019-07-17 13:54:30 -0500159}
160
Matt Spinler2813f362019-07-19 12:45:28 -0500161std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id)
162{
163 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600164 if (pel != _pelAttributes.end())
Matt Spinler2813f362019-07-19 12:45:28 -0500165 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600166 std::ifstream file{pel->second.path.c_str()};
Matt Spinler2813f362019-07-19 12:45:28 -0500167 if (!file.good())
168 {
169 auto e = errno;
170 log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600171 entry("PATH=%s", pel->second.path.c_str()));
Matt Spinler2813f362019-07-19 12:45:28 -0500172 throw file_error::Open();
173 }
174
175 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
176 std::istreambuf_iterator<char>()};
177 return data;
178 }
179
180 return std::nullopt;
181}
182
Matt Spinler1ea78802019-11-01 13:04:59 -0500183void Repository::for_each(ForEachFunc func) const
184{
Matt Spinler0ff00482019-11-06 16:19:46 -0600185 for (const auto& [id, attributes] : _pelAttributes)
Matt Spinler1ea78802019-11-01 13:04:59 -0500186 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600187 std::ifstream file{attributes.path};
Matt Spinler1ea78802019-11-01 13:04:59 -0500188
189 if (!file.good())
190 {
191 auto e = errno;
192 log<level::ERR>("Repository::for_each: Unable to open PEL file",
193 entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600194 entry("PATH=%s", attributes.path.c_str()));
Matt Spinler1ea78802019-11-01 13:04:59 -0500195 continue;
196 }
197
198 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
199 std::istreambuf_iterator<char>()};
200 file.close();
201
202 PEL pel{data};
203
204 try
205 {
206 if (func(pel))
207 {
208 break;
209 }
210 }
211 catch (std::exception& e)
212 {
213 log<level::ERR>("Repository::for_each function exception",
214 entry("ERROR=%s", e.what()));
215 }
216 }
217}
218
Matt Spinler421f6532019-11-06 15:40:45 -0600219void Repository::processAddCallbacks(const PEL& pel) const
220{
221 for (auto& [name, func] : _addSubscriptions)
222 {
223 try
224 {
225 func(pel);
226 }
227 catch (std::exception& e)
228 {
229 log<level::ERR>("PEL Repository add callback exception",
230 entry("NAME=%s", name.c_str()),
231 entry("ERROR=%s", e.what()));
232 }
233 }
234}
235
236void Repository::processDeleteCallbacks(uint32_t id) const
237{
238 for (auto& [name, func] : _deleteSubscriptions)
239 {
240 try
241 {
242 func(id);
243 }
244 catch (std::exception& e)
245 {
246 log<level::ERR>("PEL Repository delete callback exception",
247 entry("NAME=%s", name.c_str()),
248 entry("ERROR=%s", e.what()));
249 }
250 }
251}
252
Matt Spinler0ff00482019-11-06 16:19:46 -0600253std::optional<std::reference_wrapper<const Repository::PELAttributes>>
254 Repository::getPELAttributes(const LogID& id) const
255{
256 auto pel = findPEL(id);
257 if (pel != _pelAttributes.end())
258 {
259 return pel->second;
260 }
261
262 return std::nullopt;
263}
264
Matt Spinler89fa0822019-07-17 13:54:30 -0500265} // namespace pels
266} // namespace openpower