blob: 3aeecb671437fef3f80d382b40c03160918a78c8 [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 Spinler0ff00482019-11-06 16:19:46 -060061 PELAttributes attributes{dirEntry.path(),
62 pel.userHeader().actionFlags()};
63
Matt Spinler475e5742019-07-18 16:09:49 -050064 using pelID = LogID::Pel;
65 using obmcID = LogID::Obmc;
Matt Spinler0ff00482019-11-06 16:19:46 -060066 _pelAttributes.emplace(
Matt Spinler475e5742019-07-18 16:09:49 -050067 LogID(pelID(pel.id()), obmcID(pel.obmcLogID())),
Matt Spinler0ff00482019-11-06 16:19:46 -060068 attributes);
Matt Spinler475e5742019-07-18 16:09:49 -050069 }
70 else
71 {
72 log<level::ERR>(
73 "Found invalid PEL file while restoring. Removing.",
74 entry("FILENAME=%s", dirEntry.path().c_str()));
75 fs::remove(dirEntry.path());
76 }
77 }
78 catch (std::exception& e)
79 {
80 log<level::ERR>("Hit exception while restoring PEL File",
81 entry("FILENAME=%s", dirEntry.path().c_str()),
82 entry("ERROR=%s", e.what()));
83 }
84 }
Matt Spinler89fa0822019-07-17 13:54:30 -050085}
86
87std::string Repository::getPELFilename(uint32_t pelID, const BCDTime& time)
88{
89 char name[50];
90 sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", time.yearMSB,
91 time.yearLSB, time.month, time.day, time.hour, time.minutes,
92 time.seconds, time.hundredths, pelID);
93 return std::string{name};
94}
95
96void Repository::add(std::unique_ptr<PEL>& pel)
97{
98 auto path = _logPath / getPELFilename(pel->id(), pel->commitTime());
Matt Spinlerab1b97f2019-11-07 13:38:07 -060099
100 write(*(pel.get()), path);
101
102 PELAttributes attributes{path, pel->userHeader().actionFlags()};
103
104 using pelID = LogID::Pel;
105 using obmcID = LogID::Obmc;
106 _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())),
107 attributes);
108
109 processAddCallbacks(*pel);
110}
111
112void Repository::write(const PEL& pel, const fs::path& path)
113{
Matt Spinler89fa0822019-07-17 13:54:30 -0500114 std::ofstream file{path, std::ios::binary};
115
116 if (!file.good())
117 {
118 // If this fails, the filesystem is probably full so it isn't like
119 // we could successfully create yet another error log here.
120 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500121 fs::remove(path);
122 log<level::ERR>("Unable to open PEL file for writing",
123 entry("ERRNO=%d", e), entry("PATH=%s", path.c_str()));
124 throw file_error::Open();
125 }
126
Matt Spinlerab1b97f2019-11-07 13:38:07 -0600127 auto data = pel.data();
Matt Spinler89fa0822019-07-17 13:54:30 -0500128 file.write(reinterpret_cast<const char*>(data.data()), data.size());
129
130 if (file.fail())
131 {
132 // Same note as above about not being able to create an error log
133 // for this case even if we wanted.
134 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500135 file.close();
136 fs::remove(path);
137 log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e),
138 entry("PATH=%s", path.c_str()));
139 throw file_error::Write();
140 }
Matt Spinler475e5742019-07-18 16:09:49 -0500141}
142
143void Repository::remove(const LogID& id)
144{
145 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600146 if (pel != _pelAttributes.end())
Matt Spinler475e5742019-07-18 16:09:49 -0500147 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600148 fs::remove(pel->second.path);
149 _pelAttributes.erase(pel);
Matt Spinler421f6532019-11-06 15:40:45 -0600150
151 processDeleteCallbacks(id.pelID.id);
Matt Spinler475e5742019-07-18 16:09:49 -0500152 }
Matt Spinler89fa0822019-07-17 13:54:30 -0500153}
154
Matt Spinler2813f362019-07-19 12:45:28 -0500155std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id)
156{
157 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600158 if (pel != _pelAttributes.end())
Matt Spinler2813f362019-07-19 12:45:28 -0500159 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600160 std::ifstream file{pel->second.path.c_str()};
Matt Spinler2813f362019-07-19 12:45:28 -0500161 if (!file.good())
162 {
163 auto e = errno;
164 log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600165 entry("PATH=%s", pel->second.path.c_str()));
Matt Spinler2813f362019-07-19 12:45:28 -0500166 throw file_error::Open();
167 }
168
169 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
170 std::istreambuf_iterator<char>()};
171 return data;
172 }
173
174 return std::nullopt;
175}
176
Matt Spinler1ea78802019-11-01 13:04:59 -0500177void Repository::for_each(ForEachFunc func) const
178{
Matt Spinler0ff00482019-11-06 16:19:46 -0600179 for (const auto& [id, attributes] : _pelAttributes)
Matt Spinler1ea78802019-11-01 13:04:59 -0500180 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600181 std::ifstream file{attributes.path};
Matt Spinler1ea78802019-11-01 13:04:59 -0500182
183 if (!file.good())
184 {
185 auto e = errno;
186 log<level::ERR>("Repository::for_each: Unable to open PEL file",
187 entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600188 entry("PATH=%s", attributes.path.c_str()));
Matt Spinler1ea78802019-11-01 13:04:59 -0500189 continue;
190 }
191
192 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
193 std::istreambuf_iterator<char>()};
194 file.close();
195
196 PEL pel{data};
197
198 try
199 {
200 if (func(pel))
201 {
202 break;
203 }
204 }
205 catch (std::exception& e)
206 {
207 log<level::ERR>("Repository::for_each function exception",
208 entry("ERROR=%s", e.what()));
209 }
210 }
211}
212
Matt Spinler421f6532019-11-06 15:40:45 -0600213void Repository::processAddCallbacks(const PEL& pel) const
214{
215 for (auto& [name, func] : _addSubscriptions)
216 {
217 try
218 {
219 func(pel);
220 }
221 catch (std::exception& e)
222 {
223 log<level::ERR>("PEL Repository add callback exception",
224 entry("NAME=%s", name.c_str()),
225 entry("ERROR=%s", e.what()));
226 }
227 }
228}
229
230void Repository::processDeleteCallbacks(uint32_t id) const
231{
232 for (auto& [name, func] : _deleteSubscriptions)
233 {
234 try
235 {
236 func(id);
237 }
238 catch (std::exception& e)
239 {
240 log<level::ERR>("PEL Repository delete callback exception",
241 entry("NAME=%s", name.c_str()),
242 entry("ERROR=%s", e.what()));
243 }
244 }
245}
246
Matt Spinler0ff00482019-11-06 16:19:46 -0600247std::optional<std::reference_wrapper<const Repository::PELAttributes>>
248 Repository::getPELAttributes(const LogID& id) const
249{
250 auto pel = findPEL(id);
251 if (pel != _pelAttributes.end())
252 {
253 return pel->second;
254 }
255
256 return std::nullopt;
257}
258
Matt Spinler89fa0822019-07-17 13:54:30 -0500259} // namespace pels
260} // namespace openpower