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 { 61475e574dSMatt Spinler using pelID = LogID::Pel; 62475e574dSMatt Spinler using obmcID = LogID::Obmc; 63475e574dSMatt Spinler _idsToPELs.emplace( 64475e574dSMatt Spinler LogID(pelID(pel.id()), obmcID(pel.obmcLogID())), 65475e574dSMatt Spinler dirEntry.path()); 66475e574dSMatt Spinler } 67475e574dSMatt Spinler else 68475e574dSMatt Spinler { 69475e574dSMatt Spinler log<level::ERR>( 70475e574dSMatt Spinler "Found invalid PEL file while restoring. Removing.", 71475e574dSMatt Spinler entry("FILENAME=%s", dirEntry.path().c_str())); 72475e574dSMatt Spinler fs::remove(dirEntry.path()); 73475e574dSMatt Spinler } 74475e574dSMatt Spinler } 75475e574dSMatt Spinler catch (std::exception& e) 76475e574dSMatt Spinler { 77475e574dSMatt Spinler log<level::ERR>("Hit exception while restoring PEL File", 78475e574dSMatt Spinler entry("FILENAME=%s", dirEntry.path().c_str()), 79475e574dSMatt Spinler entry("ERROR=%s", e.what())); 80475e574dSMatt Spinler } 81475e574dSMatt Spinler } 8289fa082aSMatt Spinler } 8389fa082aSMatt Spinler 8489fa082aSMatt Spinler std::string Repository::getPELFilename(uint32_t pelID, const BCDTime& time) 8589fa082aSMatt Spinler { 8689fa082aSMatt Spinler char name[50]; 8789fa082aSMatt Spinler sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", time.yearMSB, 8889fa082aSMatt Spinler time.yearLSB, time.month, time.day, time.hour, time.minutes, 8989fa082aSMatt Spinler time.seconds, time.hundredths, pelID); 9089fa082aSMatt Spinler return std::string{name}; 9189fa082aSMatt Spinler } 9289fa082aSMatt Spinler 9389fa082aSMatt Spinler void Repository::add(std::unique_ptr<PEL>& pel) 9489fa082aSMatt Spinler { 9589fa082aSMatt Spinler auto path = _logPath / getPELFilename(pel->id(), pel->commitTime()); 9689fa082aSMatt Spinler std::ofstream file{path, std::ios::binary}; 9789fa082aSMatt Spinler 9889fa082aSMatt Spinler if (!file.good()) 9989fa082aSMatt Spinler { 10089fa082aSMatt Spinler // If this fails, the filesystem is probably full so it isn't like 10189fa082aSMatt Spinler // we could successfully create yet another error log here. 10289fa082aSMatt Spinler auto e = errno; 10389fa082aSMatt Spinler log<level::ERR>("Failed creating PEL file", 10489fa082aSMatt Spinler entry("FILE=%s", path.c_str())); 10589fa082aSMatt Spinler fs::remove(path); 10689fa082aSMatt Spinler log<level::ERR>("Unable to open PEL file for writing", 10789fa082aSMatt Spinler entry("ERRNO=%d", e), entry("PATH=%s", path.c_str())); 10889fa082aSMatt Spinler throw file_error::Open(); 10989fa082aSMatt Spinler } 11089fa082aSMatt Spinler 11189fa082aSMatt Spinler auto data = pel->data(); 11289fa082aSMatt Spinler file.write(reinterpret_cast<const char*>(data.data()), data.size()); 11389fa082aSMatt Spinler 11489fa082aSMatt Spinler if (file.fail()) 11589fa082aSMatt Spinler { 11689fa082aSMatt Spinler // Same note as above about not being able to create an error log 11789fa082aSMatt Spinler // for this case even if we wanted. 11889fa082aSMatt Spinler auto e = errno; 11989fa082aSMatt Spinler log<level::ERR>("Failed writing PEL file", 12089fa082aSMatt Spinler entry("FILE=%s", path.c_str())); 12189fa082aSMatt Spinler file.close(); 12289fa082aSMatt Spinler fs::remove(path); 12389fa082aSMatt Spinler log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e), 12489fa082aSMatt Spinler entry("PATH=%s", path.c_str())); 12589fa082aSMatt Spinler throw file_error::Write(); 12689fa082aSMatt Spinler } 127475e574dSMatt Spinler 128475e574dSMatt Spinler using pelID = LogID::Pel; 129475e574dSMatt Spinler using obmcID = LogID::Obmc; 130475e574dSMatt Spinler _idsToPELs.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())), path); 131*421f6531SMatt Spinler 132*421f6531SMatt Spinler processAddCallbacks(*pel); 133475e574dSMatt Spinler } 134475e574dSMatt Spinler 135475e574dSMatt Spinler void Repository::remove(const LogID& id) 136475e574dSMatt Spinler { 137475e574dSMatt Spinler auto pel = findPEL(id); 138475e574dSMatt Spinler if (pel != _idsToPELs.end()) 139475e574dSMatt Spinler { 140475e574dSMatt Spinler fs::remove(pel->second); 141475e574dSMatt Spinler _idsToPELs.erase(pel); 142*421f6531SMatt Spinler 143*421f6531SMatt Spinler processDeleteCallbacks(id.pelID.id); 144475e574dSMatt Spinler } 14589fa082aSMatt Spinler } 14689fa082aSMatt Spinler 1472813f36dSMatt Spinler std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id) 1482813f36dSMatt Spinler { 1492813f36dSMatt Spinler auto pel = findPEL(id); 1502813f36dSMatt Spinler if (pel != _idsToPELs.end()) 1512813f36dSMatt Spinler { 1522813f36dSMatt Spinler std::ifstream file{pel->second.c_str()}; 1532813f36dSMatt Spinler if (!file.good()) 1542813f36dSMatt Spinler { 1552813f36dSMatt Spinler auto e = errno; 1562813f36dSMatt Spinler log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e), 1572813f36dSMatt Spinler entry("PATH=%s", pel->second.c_str())); 1582813f36dSMatt Spinler throw file_error::Open(); 1592813f36dSMatt Spinler } 1602813f36dSMatt Spinler 1612813f36dSMatt Spinler std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), 1622813f36dSMatt Spinler std::istreambuf_iterator<char>()}; 1632813f36dSMatt Spinler return data; 1642813f36dSMatt Spinler } 1652813f36dSMatt Spinler 1662813f36dSMatt Spinler return std::nullopt; 1672813f36dSMatt Spinler } 1682813f36dSMatt Spinler 1691ea78801SMatt Spinler void Repository::for_each(ForEachFunc func) const 1701ea78801SMatt Spinler { 1711ea78801SMatt Spinler for (const auto& [id, path] : _idsToPELs) 1721ea78801SMatt Spinler { 1731ea78801SMatt Spinler std::ifstream file{path}; 1741ea78801SMatt Spinler 1751ea78801SMatt Spinler if (!file.good()) 1761ea78801SMatt Spinler { 1771ea78801SMatt Spinler auto e = errno; 1781ea78801SMatt Spinler log<level::ERR>("Repository::for_each: Unable to open PEL file", 1791ea78801SMatt Spinler entry("ERRNO=%d", e), 1801ea78801SMatt Spinler entry("PATH=%s", path.c_str())); 1811ea78801SMatt Spinler continue; 1821ea78801SMatt Spinler } 1831ea78801SMatt Spinler 1841ea78801SMatt Spinler std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), 1851ea78801SMatt Spinler std::istreambuf_iterator<char>()}; 1861ea78801SMatt Spinler file.close(); 1871ea78801SMatt Spinler 1881ea78801SMatt Spinler PEL pel{data}; 1891ea78801SMatt Spinler 1901ea78801SMatt Spinler try 1911ea78801SMatt Spinler { 1921ea78801SMatt Spinler if (func(pel)) 1931ea78801SMatt Spinler { 1941ea78801SMatt Spinler break; 1951ea78801SMatt Spinler } 1961ea78801SMatt Spinler } 1971ea78801SMatt Spinler catch (std::exception& e) 1981ea78801SMatt Spinler { 1991ea78801SMatt Spinler log<level::ERR>("Repository::for_each function exception", 2001ea78801SMatt Spinler entry("ERROR=%s", e.what())); 2011ea78801SMatt Spinler } 2021ea78801SMatt Spinler } 2031ea78801SMatt Spinler } 2041ea78801SMatt Spinler 205*421f6531SMatt Spinler void Repository::processAddCallbacks(const PEL& pel) const 206*421f6531SMatt Spinler { 207*421f6531SMatt Spinler for (auto& [name, func] : _addSubscriptions) 208*421f6531SMatt Spinler { 209*421f6531SMatt Spinler try 210*421f6531SMatt Spinler { 211*421f6531SMatt Spinler func(pel); 212*421f6531SMatt Spinler } 213*421f6531SMatt Spinler catch (std::exception& e) 214*421f6531SMatt Spinler { 215*421f6531SMatt Spinler log<level::ERR>("PEL Repository add callback exception", 216*421f6531SMatt Spinler entry("NAME=%s", name.c_str()), 217*421f6531SMatt Spinler entry("ERROR=%s", e.what())); 218*421f6531SMatt Spinler } 219*421f6531SMatt Spinler } 220*421f6531SMatt Spinler } 221*421f6531SMatt Spinler 222*421f6531SMatt Spinler void Repository::processDeleteCallbacks(uint32_t id) const 223*421f6531SMatt Spinler { 224*421f6531SMatt Spinler for (auto& [name, func] : _deleteSubscriptions) 225*421f6531SMatt Spinler { 226*421f6531SMatt Spinler try 227*421f6531SMatt Spinler { 228*421f6531SMatt Spinler func(id); 229*421f6531SMatt Spinler } 230*421f6531SMatt Spinler catch (std::exception& e) 231*421f6531SMatt Spinler { 232*421f6531SMatt Spinler log<level::ERR>("PEL Repository delete callback exception", 233*421f6531SMatt Spinler entry("NAME=%s", name.c_str()), 234*421f6531SMatt Spinler entry("ERROR=%s", e.what())); 235*421f6531SMatt Spinler } 236*421f6531SMatt Spinler } 237*421f6531SMatt Spinler } 238*421f6531SMatt Spinler 23989fa082aSMatt Spinler } // namespace pels 24089fa082aSMatt Spinler } // namespace openpower 241