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()); |
Matt Spinler | ab1b97f | 2019-11-07 13:38:07 -0600 | [diff] [blame^] | 99 | |
| 100 | write(*(pel.get()), path); |
| 101 | |
| 102 | PELAttributes attributes{path, pel->userHeader().actionFlags()}; |
| 103 | |
| 104 | using pelID = LogID::Pel; |
| 105 | using obmcID = LogID::Obmc; |
| 106 | _pelAttributes.emplace(LogID(pelID(pel->id()), obmcID(pel->obmcLogID())), |
| 107 | attributes); |
| 108 | |
| 109 | processAddCallbacks(*pel); |
| 110 | } |
| 111 | |
| 112 | void Repository::write(const PEL& pel, const fs::path& path) |
| 113 | { |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 114 | std::ofstream file{path, std::ios::binary}; |
| 115 | |
| 116 | if (!file.good()) |
| 117 | { |
| 118 | // If this fails, the filesystem is probably full so it isn't like |
| 119 | // we could successfully create yet another error log here. |
| 120 | auto e = errno; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 121 | fs::remove(path); |
| 122 | log<level::ERR>("Unable to open PEL file for writing", |
| 123 | entry("ERRNO=%d", e), entry("PATH=%s", path.c_str())); |
| 124 | throw file_error::Open(); |
| 125 | } |
| 126 | |
Matt Spinler | ab1b97f | 2019-11-07 13:38:07 -0600 | [diff] [blame^] | 127 | auto data = pel.data(); |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 128 | file.write(reinterpret_cast<const char*>(data.data()), data.size()); |
| 129 | |
| 130 | if (file.fail()) |
| 131 | { |
| 132 | // Same note as above about not being able to create an error log |
| 133 | // for this case even if we wanted. |
| 134 | auto e = errno; |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 135 | file.close(); |
| 136 | fs::remove(path); |
| 137 | log<level::ERR>("Unable to write PEL file", entry("ERRNO=%d", e), |
| 138 | entry("PATH=%s", path.c_str())); |
| 139 | throw file_error::Write(); |
| 140 | } |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void Repository::remove(const LogID& id) |
| 144 | { |
| 145 | auto pel = findPEL(id); |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 146 | if (pel != _pelAttributes.end()) |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 147 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 148 | fs::remove(pel->second.path); |
| 149 | _pelAttributes.erase(pel); |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 150 | |
| 151 | processDeleteCallbacks(id.pelID.id); |
Matt Spinler | 475e574 | 2019-07-18 16:09:49 -0500 | [diff] [blame] | 152 | } |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 153 | } |
| 154 | |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 155 | std::optional<std::vector<uint8_t>> Repository::getPELData(const LogID& id) |
| 156 | { |
| 157 | auto pel = findPEL(id); |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 158 | if (pel != _pelAttributes.end()) |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 159 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 160 | std::ifstream file{pel->second.path.c_str()}; |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 161 | if (!file.good()) |
| 162 | { |
| 163 | auto e = errno; |
| 164 | log<level::ERR>("Unable to open PEL file", entry("ERRNO=%d", e), |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 165 | entry("PATH=%s", pel->second.path.c_str())); |
Matt Spinler | 2813f36 | 2019-07-19 12:45:28 -0500 | [diff] [blame] | 166 | throw file_error::Open(); |
| 167 | } |
| 168 | |
| 169 | std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), |
| 170 | std::istreambuf_iterator<char>()}; |
| 171 | return data; |
| 172 | } |
| 173 | |
| 174 | return std::nullopt; |
| 175 | } |
| 176 | |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 177 | void Repository::for_each(ForEachFunc func) const |
| 178 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 179 | for (const auto& [id, attributes] : _pelAttributes) |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 180 | { |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 181 | std::ifstream file{attributes.path}; |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 182 | |
| 183 | if (!file.good()) |
| 184 | { |
| 185 | auto e = errno; |
| 186 | log<level::ERR>("Repository::for_each: Unable to open PEL file", |
| 187 | entry("ERRNO=%d", e), |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 188 | entry("PATH=%s", attributes.path.c_str())); |
Matt Spinler | 1ea7880 | 2019-11-01 13:04:59 -0500 | [diff] [blame] | 189 | continue; |
| 190 | } |
| 191 | |
| 192 | std::vector<uint8_t> data{std::istreambuf_iterator<char>(file), |
| 193 | std::istreambuf_iterator<char>()}; |
| 194 | file.close(); |
| 195 | |
| 196 | PEL pel{data}; |
| 197 | |
| 198 | try |
| 199 | { |
| 200 | if (func(pel)) |
| 201 | { |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | catch (std::exception& e) |
| 206 | { |
| 207 | log<level::ERR>("Repository::for_each function exception", |
| 208 | entry("ERROR=%s", e.what())); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
Matt Spinler | 421f653 | 2019-11-06 15:40:45 -0600 | [diff] [blame] | 213 | void Repository::processAddCallbacks(const PEL& pel) const |
| 214 | { |
| 215 | for (auto& [name, func] : _addSubscriptions) |
| 216 | { |
| 217 | try |
| 218 | { |
| 219 | func(pel); |
| 220 | } |
| 221 | catch (std::exception& e) |
| 222 | { |
| 223 | log<level::ERR>("PEL Repository add callback exception", |
| 224 | entry("NAME=%s", name.c_str()), |
| 225 | entry("ERROR=%s", e.what())); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void Repository::processDeleteCallbacks(uint32_t id) const |
| 231 | { |
| 232 | for (auto& [name, func] : _deleteSubscriptions) |
| 233 | { |
| 234 | try |
| 235 | { |
| 236 | func(id); |
| 237 | } |
| 238 | catch (std::exception& e) |
| 239 | { |
| 240 | log<level::ERR>("PEL Repository delete callback exception", |
| 241 | entry("NAME=%s", name.c_str()), |
| 242 | entry("ERROR=%s", e.what())); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Matt Spinler | 0ff0048 | 2019-11-06 16:19:46 -0600 | [diff] [blame] | 247 | std::optional<std::reference_wrapper<const Repository::PELAttributes>> |
| 248 | Repository::getPELAttributes(const LogID& id) const |
| 249 | { |
| 250 | auto pel = findPEL(id); |
| 251 | if (pel != _pelAttributes.end()) |
| 252 | { |
| 253 | return pel->second; |
| 254 | } |
| 255 | |
| 256 | return std::nullopt; |
| 257 | } |
| 258 | |
Matt Spinler | 89fa082 | 2019-07-17 13:54:30 -0500 | [diff] [blame] | 259 | } // namespace pels |
| 260 | } // namespace openpower |