xref: /openbmc/phosphor-logging/extensions/openpower-pels/repository.cpp (revision 0ff00485b1e8f9b57eafc2065b3b8cd82471c58d)
1711d51d8SMatt Spinler /**
2711d51d8SMatt Spinler  * Copyright © 2019 IBM Corporation
3711d51d8SMatt Spinler  *
4711d51d8SMatt Spinler  * Licensed under the Apache License, Version 2.0 (the "License");
5711d51d8SMatt Spinler  * you may not use this file except in compliance with the License.
6711d51d8SMatt Spinler  * You may obtain a copy of the License at
7711d51d8SMatt Spinler  *
8711d51d8SMatt Spinler  *     http://www.apache.org/licenses/LICENSE-2.0
9711d51d8SMatt Spinler  *
10711d51d8SMatt Spinler  * Unless required by applicable law or agreed to in writing, software
11711d51d8SMatt Spinler  * distributed under the License is distributed on an "AS IS" BASIS,
12711d51d8SMatt Spinler  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13711d51d8SMatt Spinler  * See the License for the specific language governing permissions and
14711d51d8SMatt Spinler  * limitations under the License.
15711d51d8SMatt Spinler  */
1689fa082aSMatt Spinler #include "repository.hpp"
1789fa082aSMatt Spinler 
1889fa082aSMatt Spinler #include <fstream>
1989fa082aSMatt Spinler #include <phosphor-logging/log.hpp>
2089fa082aSMatt Spinler #include <xyz/openbmc_project/Common/File/error.hpp>
2189fa082aSMatt Spinler 
2289fa082aSMatt Spinler namespace openpower
2389fa082aSMatt Spinler {
2489fa082aSMatt Spinler namespace pels
2589fa082aSMatt Spinler {
2689fa082aSMatt Spinler 
2789fa082aSMatt Spinler namespace fs = std::filesystem;
2889fa082aSMatt Spinler using namespace phosphor::logging;
2989fa082aSMatt Spinler namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
3089fa082aSMatt Spinler 
3189fa082aSMatt Spinler Repository::Repository(const std::filesystem::path& basePath) :
3289fa082aSMatt Spinler     _logPath(basePath / "logs")
3389fa082aSMatt Spinler {
3489fa082aSMatt Spinler     if (!fs::exists(_logPath))
3589fa082aSMatt Spinler     {
3689fa082aSMatt Spinler         fs::create_directories(_logPath);
3789fa082aSMatt Spinler     }
38475e574dSMatt Spinler 
39475e574dSMatt Spinler     restore();
40475e574dSMatt Spinler }
41475e574dSMatt Spinler 
42475e574dSMatt Spinler void Repository::restore()
43475e574dSMatt Spinler {
44475e574dSMatt Spinler     for (auto& dirEntry : fs::directory_iterator(_logPath))
45475e574dSMatt Spinler     {
46475e574dSMatt Spinler         try
47475e574dSMatt Spinler         {
48475e574dSMatt Spinler             if (!fs::is_regular_file(dirEntry.path()))
49475e574dSMatt Spinler             {
50475e574dSMatt Spinler                 continue;
51475e574dSMatt Spinler             }
52475e574dSMatt Spinler 
53475e574dSMatt Spinler             std::ifstream file{dirEntry.path()};
54475e574dSMatt Spinler             std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
55475e574dSMatt Spinler                                       std::istreambuf_iterator<char>()};
56475e574dSMatt Spinler             file.close();
57475e574dSMatt Spinler 
5807eefc54SMatt Spinler             PEL pel{data};
59475e574dSMatt Spinler             if (pel.valid())
60475e574dSMatt Spinler             {
61*0ff00485SMatt Spinler                 PELAttributes attributes{dirEntry.path(),
62*0ff00485SMatt Spinler                                          pel.userHeader().actionFlags()};
63*0ff00485SMatt Spinler 
64475e574dSMatt Spinler                 using pelID = LogID::Pel;
65475e574dSMatt Spinler                 using obmcID = LogID::Obmc;
66*0ff00485SMatt Spinler                 _pelAttributes.emplace(
67475e574dSMatt Spinler                     LogID(pelID(pel.id()), obmcID(pel.obmcLogID())),
68*0ff00485SMatt Spinler                     attributes);
69475e574dSMatt Spinler             }
70475e574dSMatt Spinler             else
71475e574dSMatt Spinler             {
72475e574dSMatt Spinler                 log<level::ERR>(
73475e574dSMatt Spinler                     "Found invalid PEL file while restoring.  Removing.",
74475e574dSMatt Spinler                     entry("FILENAME=%s", dirEntry.path().c_str()));
75475e574dSMatt Spinler                 fs::remove(dirEntry.path());
76475e574dSMatt Spinler             }
77475e574dSMatt Spinler         }
78475e574dSMatt Spinler         catch (std::exception& e)
79475e574dSMatt Spinler         {
80475e574dSMatt Spinler             log<level::ERR>("Hit exception while restoring PEL File",
81475e574dSMatt Spinler                             entry("FILENAME=%s", dirEntry.path().c_str()),
82475e574dSMatt Spinler                             entry("ERROR=%s", e.what()));
83475e574dSMatt Spinler         }
84475e574dSMatt Spinler     }
8589fa082aSMatt Spinler }
8689fa082aSMatt Spinler 
8789fa082aSMatt Spinler std::string Repository::getPELFilename(uint32_t pelID, const BCDTime& time)
8889fa082aSMatt Spinler {
8989fa082aSMatt Spinler     char name[50];
9089fa082aSMatt Spinler     sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", time.yearMSB,
9189fa082aSMatt Spinler             time.yearLSB, time.month, time.day, time.hour, time.minutes,
9289fa082aSMatt Spinler             time.seconds, time.hundredths, pelID);
9389fa082aSMatt Spinler     return std::string{name};
9489fa082aSMatt Spinler }
9589fa082aSMatt Spinler 
9689fa082aSMatt Spinler void Repository::add(std::unique_ptr<PEL>& pel)
9789fa082aSMatt Spinler {
9889fa082aSMatt Spinler     auto path = _logPath / getPELFilename(pel->id(), pel->commitTime());
9989fa082aSMatt Spinler     std::ofstream file{path, std::ios::binary};
10089fa082aSMatt Spinler 
10189fa082aSMatt Spinler     if (!file.good())
10289fa082aSMatt Spinler     {
10389fa082aSMatt Spinler         // If this fails, the filesystem is probably full so it isn't like
10489fa082aSMatt Spinler         // we could successfully create yet another error log here.
10589fa082aSMatt Spinler         auto e = errno;
10689fa082aSMatt Spinler         fs::remove(path);
10789fa082aSMatt Spinler         log<level::ERR>("Unable to open PEL file for writing",
10889fa082aSMatt Spinler                         entry("ERRNO=%d", e), entry("PATH=%s", path.c_str()));
10989fa082aSMatt Spinler         throw file_error::Open();
11089fa082aSMatt Spinler     }
11189fa082aSMatt Spinler 
11289fa082aSMatt Spinler     auto data = pel->data();
11389fa082aSMatt Spinler     file.write(reinterpret_cast<const char*>(data.data()), data.size());
11489fa082aSMatt Spinler 
11589fa082aSMatt Spinler     if (file.fail())
11689fa082aSMatt Spinler     {
11789fa082aSMatt Spinler         // Same note as above about not being able to create an error log
11889fa082aSMatt Spinler         // for this case even if we wanted.
11989fa082aSMatt Spinler         auto e = errno;
12089fa082aSMatt Spinler         file.close();
12189fa082aSMatt Spinler         fs::remove(path);
12289fa082aSMatt Spinler         log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e),
12389fa082aSMatt Spinler                         entry("PATH=%s", path.c_str()));
12489fa082aSMatt Spinler         throw file_error::Write();
12589fa082aSMatt Spinler     }
126475e574dSMatt Spinler 
127*0ff00485SMatt Spinler     file.close();
128*0ff00485SMatt Spinler 
129*0ff00485SMatt Spinler     PELAttributes attributes{path, pel->userHeader().actionFlags()};
130*0ff00485SMatt Spinler 
131475e574dSMatt Spinler     using pelID = LogID::Pel;
132475e574dSMatt Spinler     using obmcID = LogID::Obmc;
133*0ff00485SMatt Spinler     _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())),
134*0ff00485SMatt Spinler                            attributes);
135421f6531SMatt Spinler 
136421f6531SMatt Spinler     processAddCallbacks(*pel);
137475e574dSMatt Spinler }
138475e574dSMatt Spinler 
139475e574dSMatt Spinler void Repository::remove(const LogID& id)
140475e574dSMatt Spinler {
141475e574dSMatt Spinler     auto pel = findPEL(id);
142*0ff00485SMatt Spinler     if (pel != _pelAttributes.end())
143475e574dSMatt Spinler     {
144*0ff00485SMatt Spinler         fs::remove(pel->second.path);
145*0ff00485SMatt Spinler         _pelAttributes.erase(pel);
146421f6531SMatt Spinler 
147421f6531SMatt Spinler         processDeleteCallbacks(id.pelID.id);
148475e574dSMatt Spinler     }
14989fa082aSMatt Spinler }
15089fa082aSMatt Spinler 
1512813f36dSMatt Spinler std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id)
1522813f36dSMatt Spinler {
1532813f36dSMatt Spinler     auto pel = findPEL(id);
154*0ff00485SMatt Spinler     if (pel != _pelAttributes.end())
1552813f36dSMatt Spinler     {
156*0ff00485SMatt Spinler         std::ifstream file{pel->second.path.c_str()};
1572813f36dSMatt Spinler         if (!file.good())
1582813f36dSMatt Spinler         {
1592813f36dSMatt Spinler             auto e = errno;
1602813f36dSMatt Spinler             log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e),
161*0ff00485SMatt Spinler                             entry("PATH=%s", pel->second.path.c_str()));
1622813f36dSMatt Spinler             throw file_error::Open();
1632813f36dSMatt Spinler         }
1642813f36dSMatt Spinler 
1652813f36dSMatt Spinler         std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
1662813f36dSMatt Spinler                                   std::istreambuf_iterator<char>()};
1672813f36dSMatt Spinler         return data;
1682813f36dSMatt Spinler     }
1692813f36dSMatt Spinler 
1702813f36dSMatt Spinler     return std::nullopt;
1712813f36dSMatt Spinler }
1722813f36dSMatt Spinler 
1731ea78801SMatt Spinler void Repository::for_each(ForEachFunc func) const
1741ea78801SMatt Spinler {
175*0ff00485SMatt Spinler     for (const auto& [id, attributes] : _pelAttributes)
1761ea78801SMatt Spinler     {
177*0ff00485SMatt Spinler         std::ifstream file{attributes.path};
1781ea78801SMatt Spinler 
1791ea78801SMatt Spinler         if (!file.good())
1801ea78801SMatt Spinler         {
1811ea78801SMatt Spinler             auto e = errno;
1821ea78801SMatt Spinler             log<level::ERR>("Repository::for_each: Unable to open PEL file",
1831ea78801SMatt Spinler                             entry("ERRNO=%d", e),
184*0ff00485SMatt Spinler                             entry("PATH=%s", attributes.path.c_str()));
1851ea78801SMatt Spinler             continue;
1861ea78801SMatt Spinler         }
1871ea78801SMatt Spinler 
1881ea78801SMatt Spinler         std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
1891ea78801SMatt Spinler                                   std::istreambuf_iterator<char>()};
1901ea78801SMatt Spinler         file.close();
1911ea78801SMatt Spinler 
1921ea78801SMatt Spinler         PEL pel{data};
1931ea78801SMatt Spinler 
1941ea78801SMatt Spinler         try
1951ea78801SMatt Spinler         {
1961ea78801SMatt Spinler             if (func(pel))
1971ea78801SMatt Spinler             {
1981ea78801SMatt Spinler                 break;
1991ea78801SMatt Spinler             }
2001ea78801SMatt Spinler         }
2011ea78801SMatt Spinler         catch (std::exception& e)
2021ea78801SMatt Spinler         {
2031ea78801SMatt Spinler             log<level::ERR>("Repository::for_each function exception",
2041ea78801SMatt Spinler                             entry("ERROR=%s", e.what()));
2051ea78801SMatt Spinler         }
2061ea78801SMatt Spinler     }
2071ea78801SMatt Spinler }
2081ea78801SMatt Spinler 
209421f6531SMatt Spinler void Repository::processAddCallbacks(const PEL& pel) const
210421f6531SMatt Spinler {
211421f6531SMatt Spinler     for (auto& [name, func] : _addSubscriptions)
212421f6531SMatt Spinler     {
213421f6531SMatt Spinler         try
214421f6531SMatt Spinler         {
215421f6531SMatt Spinler             func(pel);
216421f6531SMatt Spinler         }
217421f6531SMatt Spinler         catch (std::exception& e)
218421f6531SMatt Spinler         {
219421f6531SMatt Spinler             log<level::ERR>("PEL Repository add callback exception",
220421f6531SMatt Spinler                             entry("NAME=%s", name.c_str()),
221421f6531SMatt Spinler                             entry("ERROR=%s", e.what()));
222421f6531SMatt Spinler         }
223421f6531SMatt Spinler     }
224421f6531SMatt Spinler }
225421f6531SMatt Spinler 
226421f6531SMatt Spinler void Repository::processDeleteCallbacks(uint32_t id) const
227421f6531SMatt Spinler {
228421f6531SMatt Spinler     for (auto& [name, func] : _deleteSubscriptions)
229421f6531SMatt Spinler     {
230421f6531SMatt Spinler         try
231421f6531SMatt Spinler         {
232421f6531SMatt Spinler             func(id);
233421f6531SMatt Spinler         }
234421f6531SMatt Spinler         catch (std::exception& e)
235421f6531SMatt Spinler         {
236421f6531SMatt Spinler             log<level::ERR>("PEL Repository delete callback exception",
237421f6531SMatt Spinler                             entry("NAME=%s", name.c_str()),
238421f6531SMatt Spinler                             entry("ERROR=%s", e.what()));
239421f6531SMatt Spinler         }
240421f6531SMatt Spinler     }
241421f6531SMatt Spinler }
242421f6531SMatt Spinler 
243*0ff00485SMatt Spinler std::optional<std::reference_wrapper<const Repository::PELAttributes>>
244*0ff00485SMatt Spinler     Repository::getPELAttributes(const LogID& id) const
245*0ff00485SMatt Spinler {
246*0ff00485SMatt Spinler     auto pel = findPEL(id);
247*0ff00485SMatt Spinler     if (pel != _pelAttributes.end())
248*0ff00485SMatt Spinler     {
249*0ff00485SMatt Spinler         return pel->second;
250*0ff00485SMatt Spinler     }
251*0ff00485SMatt Spinler 
252*0ff00485SMatt Spinler     return std::nullopt;
253*0ff00485SMatt Spinler }
254*0ff00485SMatt Spinler 
25589fa082aSMatt Spinler } // namespace pels
25689fa082aSMatt Spinler } // namespace openpower
257