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); 131475e574dSMatt Spinler } 132475e574dSMatt Spinler 133475e574dSMatt Spinler void Repository::remove(const LogID& id) 134475e574dSMatt Spinler { 135475e574dSMatt Spinler auto pel = findPEL(id); 136475e574dSMatt Spinler if (pel != _idsToPELs.end()) 137475e574dSMatt Spinler { 138475e574dSMatt Spinler fs::remove(pel->second); 139475e574dSMatt Spinler _idsToPELs.erase(pel); 140475e574dSMatt Spinler } 14189fa082aSMatt Spinler } 14289fa082aSMatt Spinler 1432813f36dSMatt Spinler std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id) 1442813f36dSMatt Spinler { 1452813f36dSMatt Spinler auto pel = findPEL(id); 1462813f36dSMatt Spinler if (pel != _idsToPELs.end()) 1472813f36dSMatt Spinler { 1482813f36dSMatt Spinler std::ifstream file{pel->second.c_str()}; 1492813f36dSMatt Spinler if (!file.good()) 1502813f36dSMatt Spinler { 1512813f36dSMatt Spinler auto e = errno; 1522813f36dSMatt Spinler log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e), 1532813f36dSMatt Spinler entry("PATH=%s", pel->second.c_str())); 1542813f36dSMatt Spinler throw file_error::Open(); 1552813f36dSMatt Spinler } 1562813f36dSMatt Spinler 1572813f36dSMatt Spinler std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), 1582813f36dSMatt Spinler std::istreambuf_iterator<char>()}; 1592813f36dSMatt Spinler return data; 1602813f36dSMatt Spinler } 1612813f36dSMatt Spinler 1622813f36dSMatt Spinler return std::nullopt; 1632813f36dSMatt Spinler } 1642813f36dSMatt Spinler 165*1ea78801SMatt Spinler void Repository::for_each(ForEachFunc func) const 166*1ea78801SMatt Spinler { 167*1ea78801SMatt Spinler for (const auto& [id, path] : _idsToPELs) 168*1ea78801SMatt Spinler { 169*1ea78801SMatt Spinler std::ifstream file{path}; 170*1ea78801SMatt Spinler 171*1ea78801SMatt Spinler if (!file.good()) 172*1ea78801SMatt Spinler { 173*1ea78801SMatt Spinler auto e = errno; 174*1ea78801SMatt Spinler log<level::ERR>("Repository::for_each: Unable to open PEL file", 175*1ea78801SMatt Spinler entry("ERRNO=%d", e), 176*1ea78801SMatt Spinler entry("PATH=%s", path.c_str())); 177*1ea78801SMatt Spinler continue; 178*1ea78801SMatt Spinler } 179*1ea78801SMatt Spinler 180*1ea78801SMatt Spinler std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), 181*1ea78801SMatt Spinler std::istreambuf_iterator<char>()}; 182*1ea78801SMatt Spinler file.close(); 183*1ea78801SMatt Spinler 184*1ea78801SMatt Spinler PEL pel{data}; 185*1ea78801SMatt Spinler 186*1ea78801SMatt Spinler try 187*1ea78801SMatt Spinler { 188*1ea78801SMatt Spinler if (func(pel)) 189*1ea78801SMatt Spinler { 190*1ea78801SMatt Spinler break; 191*1ea78801SMatt Spinler } 192*1ea78801SMatt Spinler } 193*1ea78801SMatt Spinler catch (std::exception& e) 194*1ea78801SMatt Spinler { 195*1ea78801SMatt Spinler log<level::ERR>("Repository::for_each function exception", 196*1ea78801SMatt Spinler entry("ERROR=%s", e.what())); 197*1ea78801SMatt Spinler } 198*1ea78801SMatt Spinler } 199*1ea78801SMatt Spinler } 200*1ea78801SMatt Spinler 20189fa082aSMatt Spinler } // namespace pels 20289fa082aSMatt Spinler } // namespace openpower 203