blob: 93d2f81b616fa753c9aafe59271dc08d12f0f32b [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());
99 std::ofstream file{path, std::ios::binary};
100
101 if (!file.good())
102 {
103 // If this fails, the filesystem is probably full so it isn't like
104 // we could successfully create yet another error log here.
105 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500106 fs::remove(path);
107 log<level::ERR>("Unable to open PEL file for writing",
108 entry("ERRNO=%d", e), entry("PATH=%s", path.c_str()));
109 throw file_error::Open();
110 }
111
112 auto data = pel->data();
113 file.write(reinterpret_cast<const char*>(data.data()), data.size());
114
115 if (file.fail())
116 {
117 // Same note as above about not being able to create an error log
118 // for this case even if we wanted.
119 auto e = errno;
Matt Spinler89fa0822019-07-17 13:54:30 -0500120 file.close();
121 fs::remove(path);
122 log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e),
123 entry("PATH=%s", path.c_str()));
124 throw file_error::Write();
125 }
Matt Spinler475e5742019-07-18 16:09:49 -0500126
Matt Spinler0ff00482019-11-06 16:19:46 -0600127 file.close();
128
129 PELAttributes attributes{path, pel->userHeader().actionFlags()};
130
Matt Spinler475e5742019-07-18 16:09:49 -0500131 using pelID = LogID::Pel;
132 using obmcID = LogID::Obmc;
Matt Spinler0ff00482019-11-06 16:19:46 -0600133 _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())),
134 attributes);
Matt Spinler421f6532019-11-06 15:40:45 -0600135
136 processAddCallbacks(*pel);
Matt Spinler475e5742019-07-18 16:09:49 -0500137}
138
139void Repository::remove(const LogID& id)
140{
141 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600142 if (pel != _pelAttributes.end())
Matt Spinler475e5742019-07-18 16:09:49 -0500143 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600144 fs::remove(pel->second.path);
145 _pelAttributes.erase(pel);
Matt Spinler421f6532019-11-06 15:40:45 -0600146
147 processDeleteCallbacks(id.pelID.id);
Matt Spinler475e5742019-07-18 16:09:49 -0500148 }
Matt Spinler89fa0822019-07-17 13:54:30 -0500149}
150
Matt Spinler2813f362019-07-19 12:45:28 -0500151std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id)
152{
153 auto pel = findPEL(id);
Matt Spinler0ff00482019-11-06 16:19:46 -0600154 if (pel != _pelAttributes.end())
Matt Spinler2813f362019-07-19 12:45:28 -0500155 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600156 std::ifstream file{pel->second.path.c_str()};
Matt Spinler2813f362019-07-19 12:45:28 -0500157 if (!file.good())
158 {
159 auto e = errno;
160 log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600161 entry("PATH=%s", pel->second.path.c_str()));
Matt Spinler2813f362019-07-19 12:45:28 -0500162 throw file_error::Open();
163 }
164
165 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
166 std::istreambuf_iterator<char>()};
167 return data;
168 }
169
170 return std::nullopt;
171}
172
Matt Spinler1ea78802019-11-01 13:04:59 -0500173void Repository::for_each(ForEachFunc func) const
174{
Matt Spinler0ff00482019-11-06 16:19:46 -0600175 for (const auto& [id, attributes] : _pelAttributes)
Matt Spinler1ea78802019-11-01 13:04:59 -0500176 {
Matt Spinler0ff00482019-11-06 16:19:46 -0600177 std::ifstream file{attributes.path};
Matt Spinler1ea78802019-11-01 13:04:59 -0500178
179 if (!file.good())
180 {
181 auto e = errno;
182 log<level::ERR>("Repository::for_each: Unable to open PEL file",
183 entry("ERRNO=%d", e),
Matt Spinler0ff00482019-11-06 16:19:46 -0600184 entry("PATH=%s", attributes.path.c_str()));
Matt Spinler1ea78802019-11-01 13:04:59 -0500185 continue;
186 }
187
188 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
189 std::istreambuf_iterator<char>()};
190 file.close();
191
192 PEL pel{data};
193
194 try
195 {
196 if (func(pel))
197 {
198 break;
199 }
200 }
201 catch (std::exception& e)
202 {
203 log<level::ERR>("Repository::for_each function exception",
204 entry("ERROR=%s", e.what()));
205 }
206 }
207}
208
Matt Spinler421f6532019-11-06 15:40:45 -0600209void Repository::processAddCallbacks(const PEL& pel) const
210{
211 for (auto& [name, func] : _addSubscriptions)
212 {
213 try
214 {
215 func(pel);
216 }
217 catch (std::exception& e)
218 {
219 log<level::ERR>("PEL Repository add callback exception",
220 entry("NAME=%s", name.c_str()),
221 entry("ERROR=%s", e.what()));
222 }
223 }
224}
225
226void Repository::processDeleteCallbacks(uint32_t id) const
227{
228 for (auto& [name, func] : _deleteSubscriptions)
229 {
230 try
231 {
232 func(id);
233 }
234 catch (std::exception& e)
235 {
236 log<level::ERR>("PEL Repository delete callback exception",
237 entry("NAME=%s", name.c_str()),
238 entry("ERROR=%s", e.what()));
239 }
240 }
241}
242
Matt Spinler0ff00482019-11-06 16:19:46 -0600243std::optional<std::reference_wrapper<const Repository::PELAttributes>>
244 Repository::getPELAttributes(const LogID& id) const
245{
246 auto pel = findPEL(id);
247 if (pel != _pelAttributes.end())
248 {
249 return pel->second;
250 }
251
252 return std::nullopt;
253}
254
Matt Spinler89fa0822019-07-17 13:54:30 -0500255} // namespace pels
256} // namespace openpower