Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 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 Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 16 | #include "repository.hpp" |
| 17 | |
| 18 | #include <fstream> |
| 19 | #include <phosphor-logging/log.hpp> |
| 20 | #include <xyz/openbmc_project/Common/File/error.hpp> |
| 21 | |
| 22 | namespace openpower |
| 23 | { |
| 24 | namespace pels |
| 25 | { |
| 26 | |
| 27 | namespace fs = std::filesystem; |
| 28 | using namespace phosphor::logging; |
| 29 | namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error; |
| 30 | |
| 31 | Repository::Repository(const std::filesystem::path& basePath) : |
| 32 | _logPath(basePath / "logs") |
| 33 | { |
| 34 | if (!fs::exists(_logPath)) |
| 35 | { |
| 36 | fs::create_directories(_logPath); |
| 37 | } |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 38 | |
| 39 | restore(); |
| 40 | } |
| 41 | |
| 42 | void 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 Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 58 | PEL pel{data}; |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 59 | if (pel.valid()) |
| 60 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 61 | PELAttributes attributes{dirEntry.path(), |
| 62 | pel.userHeader().actionFlags()}; |
| 63 | |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 64 | using pelID = LogID::Pel; |
| 65 | using obmcID = LogID::Obmc; |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 66 | _pelAttributes.emplace( |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 67 | LogID(pelID(pel.id()), obmcID(pel.obmcLogID())), |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 68 | attributes); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 69 | } |
| 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 Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | std::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 | |
| 96 | void 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 Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 106 | 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 Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 120 | 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 Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 126 | |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 127 | file.close(); |
| 128 | |
| 129 | PELAttributes attributes{path, pel->userHeader().actionFlags()}; |
| 130 | |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 131 | using pelID = LogID::Pel; |
| 132 | using obmcID = LogID::Obmc; |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 133 | _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())), |
| 134 | attributes); |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 135 | |
| 136 | processAddCallbacks(*pel); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void Repository::remove(const LogID& id) |
| 140 | { |
| 141 | auto pel = findPEL(id); |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 142 | if (pel != _pelAttributes.end()) |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 143 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 144 | fs::remove(pel->second.path); |
| 145 | _pelAttributes.erase(pel); |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 146 | |
| 147 | processDeleteCallbacks(id.pelID.id); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 148 | } |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 149 | } |
| 150 | |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 151 | std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id) |
| 152 | { |
| 153 | auto pel = findPEL(id); |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 154 | if (pel != _pelAttributes.end()) |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 155 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 156 | std::ifstream file{pel->second.path.c_str()}; |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 157 | if (!file.good()) |
| 158 | { |
| 159 | auto e = errno; |
| 160 | log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e), |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 161 | entry("PATH=%s", pel->second.path.c_str())); |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 162 | 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 Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 173 | void Repository::for_each(ForEachFunc func) const |
| 174 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 175 | for (const auto& [id, attributes] : _pelAttributes) |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 176 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 177 | std::ifstream file{attributes.path}; |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 178 | |
| 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 Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 184 | entry("PATH=%s", attributes.path.c_str())); |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 185 | 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 Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 209 | void 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 | |
| 226 | void 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 Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 243 | std::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 Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 255 | } // namespace pels |
| 256 | } // namespace openpower |