Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "log_manager.hpp" |
| 4 | |
| 5 | #include "elog_entry.hpp" |
| 6 | #include "elog_meta.hpp" |
| 7 | #include "elog_serialize.hpp" |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 8 | #include "extensions.hpp" |
Matt Spinler | f61f292 | 2020-06-23 11:32:49 -0500 | [diff] [blame] | 9 | #include "util.hpp" |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 10 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 11 | #include <poll.h> |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 12 | #include <sys/inotify.h> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 13 | #include <systemd/sd-bus.h> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 14 | #include <systemd/sd-journal.h> |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 15 | #include <unistd.h> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 16 | |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 17 | #include <cassert> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 18 | #include <chrono> |
| 19 | #include <cstdio> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 20 | #include <cstring> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 21 | #include <fstream> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 22 | #include <functional> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 23 | #include <future> |
| 24 | #include <iostream> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 25 | #include <map> |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 26 | #include <phosphor-logging/log.hpp> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 27 | #include <sdbusplus/vtable.hpp> |
| 28 | #include <set> |
| 29 | #include <string> |
| 30 | #include <vector> |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 31 | #include <xyz/openbmc_project/State/Host/server.hpp> |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 32 | |
| 33 | using namespace phosphor::logging; |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 34 | using namespace std::chrono; |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 35 | using sdbusplus::exception::SdBusError; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 36 | extern const std::map<metadata::Metadata, |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 37 | std::function<metadata::associations::Type>> |
| 38 | meta; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 39 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 40 | namespace phosphor |
| 41 | { |
| 42 | namespace logging |
| 43 | { |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 44 | namespace internal |
| 45 | { |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 46 | inline auto getLevel(const std::string& errMsg) |
| 47 | { |
| 48 | auto reqLevel = Entry::Level::Error; // Default to Error |
| 49 | |
| 50 | auto levelmap = g_errLevelMap.find(errMsg); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 51 | if (levelmap != g_errLevelMap.end()) |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 52 | { |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 53 | reqLevel = static_cast<Entry::Level>(levelmap->second); |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 56 | return reqLevel; |
| 57 | } |
| 58 | |
Nagaraju Goruganti | 477b731 | 2018-06-25 23:28:58 -0500 | [diff] [blame] | 59 | int Manager::getRealErrSize() |
| 60 | { |
| 61 | return realErrors.size(); |
| 62 | } |
| 63 | |
| 64 | int Manager::getInfoErrSize() |
| 65 | { |
| 66 | return infoErrors.size(); |
| 67 | } |
| 68 | |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 69 | uint32_t Manager::commit(uint64_t transactionId, std::string errMsg) |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 70 | { |
| 71 | auto level = getLevel(errMsg); |
| 72 | _commit(transactionId, std::move(errMsg), level); |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 73 | return entryId; |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 74 | } |
| 75 | |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 76 | uint32_t Manager::commitWithLvl(uint64_t transactionId, std::string errMsg, |
| 77 | uint32_t errLvl) |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 78 | { |
| 79 | _commit(transactionId, std::move(errMsg), |
| 80 | static_cast<Entry::Level>(errLvl)); |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 81 | return entryId; |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Patrick Williams | a517197 | 2021-04-16 20:10:01 -0500 | [diff] [blame] | 84 | void Manager::_commit(uint64_t transactionId [[maybe_unused]], |
| 85 | std::string&& errMsg, Entry::Level errLvl) |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 86 | { |
Patrick Williams | a517197 | 2021-04-16 20:10:01 -0500 | [diff] [blame] | 87 | std::vector<std::string> additionalData{}; |
| 88 | |
| 89 | // When running as a test-case, the system may have a LOT of journal |
| 90 | // data and we may not have permissions to do some of the journal sync |
| 91 | // operations. Just skip over them. |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 92 | if (!IS_UNIT_TEST) |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 93 | { |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 94 | constexpr const auto transactionIdVar = "TRANSACTION_ID"; |
| 95 | // Length of 'TRANSACTION_ID' string. |
| 96 | constexpr const auto transactionIdVarSize = |
| 97 | std::strlen(transactionIdVar); |
| 98 | // Length of 'TRANSACTION_ID=' string. |
| 99 | constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 100 | |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 101 | // Flush all the pending log messages into the journal |
| 102 | journalSync(); |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 103 | |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 104 | sd_journal* j = nullptr; |
| 105 | int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 106 | if (rc < 0) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 107 | { |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 108 | logging::log<logging::level::ERR>( |
| 109 | "Failed to open journal", |
| 110 | logging::entry("DESCRIPTION=%s", strerror(-rc))); |
| 111 | return; |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 112 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 113 | |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 114 | std::string transactionIdStr = std::to_string(transactionId); |
| 115 | std::set<std::string> metalist; |
| 116 | auto metamap = g_errMetaMap.find(errMsg); |
| 117 | if (metamap != g_errMetaMap.end()) |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 118 | { |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 119 | metalist.insert(metamap->second.begin(), metamap->second.end()); |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 120 | } |
| 121 | |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 122 | // Add _PID field information in AdditionalData. |
| 123 | metalist.insert("_PID"); |
| 124 | |
| 125 | // Read the journal from the end to get the most recent entry first. |
| 126 | // The result from the sd_journal_get_data() is of the form |
| 127 | // VARIABLE=value. |
| 128 | SD_JOURNAL_FOREACH_BACKWARDS(j) |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 129 | { |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 130 | const char* data = nullptr; |
| 131 | size_t length = 0; |
| 132 | |
| 133 | // Look for the transaction id metadata variable |
| 134 | rc = sd_journal_get_data(j, transactionIdVar, (const void**)&data, |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 135 | &length); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 136 | if (rc < 0) |
| 137 | { |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 138 | // This journal entry does not have the TRANSACTION_ID |
| 139 | // metadata variable. |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 140 | continue; |
| 141 | } |
| 142 | |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 143 | // journald does not guarantee that sd_journal_get_data() returns |
| 144 | // NULL terminated strings, so need to specify the size to use to |
| 145 | // compare, use the returned length instead of anything that relies |
| 146 | // on NULL terminators like strlen(). The data variable is in the |
| 147 | // form of 'TRANSACTION_ID=1234'. Remove the TRANSACTION_ID |
| 148 | // characters plus the (=) sign to do the comparison. 'data + |
| 149 | // transactionIdVarOffset' will be in the form of '1234'. 'length - |
| 150 | // transactionIdVarOffset' will be the length of '1234'. |
| 151 | if ((length <= (transactionIdVarOffset)) || |
| 152 | (transactionIdStr.compare( |
| 153 | 0, transactionIdStr.size(), data + transactionIdVarOffset, |
| 154 | length - transactionIdVarOffset) != 0)) |
| 155 | { |
| 156 | // The value of the TRANSACTION_ID metadata is not the requested |
| 157 | // transaction id number. |
| 158 | continue; |
| 159 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 160 | |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 161 | // Search for all metadata variables in the current journal entry. |
| 162 | for (auto i = metalist.cbegin(); i != metalist.cend();) |
| 163 | { |
| 164 | rc = sd_journal_get_data(j, (*i).c_str(), (const void**)&data, |
| 165 | &length); |
| 166 | if (rc < 0) |
| 167 | { |
| 168 | // Metadata variable not found, check next metadata |
| 169 | // variable. |
| 170 | i++; |
| 171 | continue; |
| 172 | } |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 173 | |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 174 | // Metadata variable found, save it and remove it from the set. |
| 175 | additionalData.emplace_back(data, length); |
| 176 | i = metalist.erase(i); |
| 177 | } |
| 178 | if (metalist.empty()) |
| 179 | { |
| 180 | // All metadata variables found, break out of journal loop. |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | if (!metalist.empty()) |
| 185 | { |
| 186 | // Not all the metadata variables were found in the journal. |
| 187 | for (auto& metaVarStr : metalist) |
| 188 | { |
| 189 | logging::log<logging::level::INFO>( |
| 190 | "Failed to find metadata", |
| 191 | logging::entry("META_FIELD=%s", metaVarStr.c_str())); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | sd_journal_close(j); |
| 196 | } |
Matt Spinler | b60e755 | 2019-07-24 15:28:08 -0500 | [diff] [blame] | 197 | createEntry(errMsg, errLvl, additionalData); |
| 198 | } |
| 199 | |
| 200 | void Manager::createEntry(std::string errMsg, Entry::Level errLvl, |
Matt Spinler | c64b712 | 2020-03-26 10:55:01 -0500 | [diff] [blame] | 201 | std::vector<std::string> additionalData, |
| 202 | const FFDCEntries& ffdc) |
Matt Spinler | b60e755 | 2019-07-24 15:28:08 -0500 | [diff] [blame] | 203 | { |
| 204 | if (!Extensions::disableDefaultLogCaps()) |
| 205 | { |
| 206 | if (errLvl < Entry::sevLowerLimit) |
| 207 | { |
| 208 | if (realErrors.size() >= ERROR_CAP) |
| 209 | { |
| 210 | erase(realErrors.front()); |
| 211 | } |
| 212 | } |
| 213 | else |
| 214 | { |
| 215 | if (infoErrors.size() >= ERROR_INFO_CAP) |
| 216 | { |
| 217 | erase(infoErrors.front()); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 222 | entryId++; |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 223 | if (errLvl >= Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 224 | { |
| 225 | infoErrors.push_back(entryId); |
| 226 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 227 | else |
| 228 | { |
| 229 | realErrors.push_back(entryId); |
| 230 | } |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 231 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 232 | std::chrono::system_clock::now().time_since_epoch()) |
| 233 | .count(); |
| 234 | auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 235 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 236 | AssociationList objects{}; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 237 | processMetadata(errMsg, additionalData, objects); |
| 238 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 239 | auto e = std::make_unique<Entry>(busLog, objPath, entryId, |
| 240 | ms, // Milliseconds since 1970 |
| 241 | errLvl, std::move(errMsg), |
| 242 | std::move(additionalData), |
| 243 | std::move(objects), fwVersion, *this); |
Adriana Kobylak | 1ff95ef | 2020-12-03 13:52:19 -0600 | [diff] [blame] | 244 | auto path = serialize(*e); |
| 245 | e->path(path); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 246 | |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 247 | if (isQuiesceOnErrorEnabled() && isCalloutPresent(*e)) |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 248 | { |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 249 | quiesceOnError(entryId); |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Adriana Kobylak | e7d271a | 2020-12-07 14:32:44 -0600 | [diff] [blame] | 252 | // Add entry before calling the extensions so that they have access to it |
| 253 | entries.insert(std::make_pair(entryId, std::move(e))); |
| 254 | |
| 255 | doExtensionLogCreate(*entries.find(entryId)->second, ffdc); |
Matt Spinler | c64b712 | 2020-03-26 10:55:01 -0500 | [diff] [blame] | 256 | |
| 257 | // Note: No need to close the file descriptors in the FFDC. |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 258 | } |
| 259 | |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 260 | bool Manager::isQuiesceOnErrorEnabled() |
| 261 | { |
Patrick Williams | a517197 | 2021-04-16 20:10:01 -0500 | [diff] [blame] | 262 | // When running under tests, the Logging.Settings service will not be |
| 263 | // present. Assume false. |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 264 | if (IS_UNIT_TEST) |
| 265 | { |
| 266 | return false; |
| 267 | } |
Patrick Williams | a517197 | 2021-04-16 20:10:01 -0500 | [diff] [blame] | 268 | |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 269 | std::variant<bool> property; |
| 270 | |
| 271 | auto method = this->busLog.new_method_call( |
| 272 | "xyz.openbmc_project.Settings", "/xyz/openbmc_project/logging/settings", |
| 273 | "org.freedesktop.DBus.Properties", "Get"); |
| 274 | |
| 275 | method.append("xyz.openbmc_project.Logging.Settings", "QuiesceOnHwError"); |
| 276 | |
| 277 | try |
| 278 | { |
| 279 | auto reply = this->busLog.call(method); |
| 280 | reply.read(property); |
| 281 | } |
| 282 | catch (const SdBusError& e) |
| 283 | { |
| 284 | log<level::ERR>("Error reading QuiesceOnHwError property", |
| 285 | entry("ERROR=%s", e.what())); |
| 286 | throw; |
| 287 | } |
| 288 | |
| 289 | return std::get<bool>(property); |
| 290 | } |
| 291 | |
Andrew Geissler | e4960ee | 2020-03-30 14:31:52 -0500 | [diff] [blame] | 292 | bool Manager::isCalloutPresent(const Entry& entry) |
| 293 | { |
| 294 | for (const auto& c : entry.additionalData()) |
| 295 | { |
| 296 | if (c.find("CALLOUT_") != std::string::npos) |
| 297 | { |
| 298 | return true; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return false; |
| 303 | } |
| 304 | |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 305 | void Manager::findAndRemoveResolvedBlocks() |
| 306 | { |
| 307 | for (auto& entry : entries) |
| 308 | { |
| 309 | if (entry.second->resolved()) |
| 310 | { |
| 311 | checkAndRemoveBlockingError(entry.first); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void Manager::onEntryResolve(sdbusplus::message::message& msg) |
| 317 | { |
| 318 | using Interface = std::string; |
| 319 | using Property = std::string; |
| 320 | using Value = std::string; |
Patrick Williams | 25acc6c | 2020-06-02 11:05:34 -0500 | [diff] [blame] | 321 | using Properties = std::map<Property, std::variant<Value>>; |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 322 | |
| 323 | Interface interface; |
| 324 | Properties properties; |
| 325 | |
| 326 | msg.read(interface, properties); |
| 327 | |
| 328 | for (const auto& p : properties) |
| 329 | { |
| 330 | if (p.first == "Resolved") |
| 331 | { |
| 332 | findAndRemoveResolvedBlocks(); |
| 333 | return; |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 338 | void Manager::checkAndQuiesceHost() |
| 339 | { |
| 340 | // First check host state |
| 341 | std::variant<std::string> property; |
| 342 | |
| 343 | auto method = this->busLog.new_method_call( |
| 344 | "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0", |
| 345 | "org.freedesktop.DBus.Properties", "Get"); |
| 346 | |
| 347 | method.append("xyz.openbmc_project.State.Host", "CurrentHostState"); |
| 348 | |
| 349 | try |
| 350 | { |
| 351 | auto reply = this->busLog.call(method); |
| 352 | reply.read(property); |
| 353 | } |
| 354 | catch (const SdBusError& e) |
| 355 | { |
| 356 | // Quiescing the host is a "best effort" type function. If unable to |
| 357 | // read the host state or it comes back empty, just return. |
| 358 | // The boot block object will still be created and the associations to |
| 359 | // find the log will be present. Don't want a dependency with |
| 360 | // phosphor-state-manager service |
| 361 | log<level::INFO>("Error reading QuiesceOnHwError property", |
| 362 | entry("ERROR=%s", e.what())); |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | std::string hostState = std::get<std::string>(property); |
| 367 | |
| 368 | // If host state is empty, do nothing |
| 369 | if (hostState.empty()) |
| 370 | { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | using Host = sdbusplus::xyz::openbmc_project::State::server::Host; |
| 375 | auto state = Host::convertHostStateFromString(hostState); |
| 376 | if (state != Host::HostState::Running) |
| 377 | { |
| 378 | return; |
| 379 | } |
| 380 | |
| 381 | auto quiesce = this->busLog.new_method_call( |
| 382 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 383 | "org.freedesktop.systemd1.Manager", "StartUnit"); |
| 384 | |
| 385 | quiesce.append("obmc-host-quiesce@0.target"); |
| 386 | quiesce.append("replace"); |
| 387 | |
| 388 | this->busLog.call_noreply(quiesce); |
| 389 | } |
| 390 | |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 391 | void Manager::quiesceOnError(const uint32_t entryId) |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 392 | { |
Andrew Geissler | 72a1cca | 2020-09-10 16:49:09 -0500 | [diff] [blame] | 393 | // Verify we don't already have this entry blocking |
| 394 | auto it = find_if( |
| 395 | this->blockingErrors.begin(), this->blockingErrors.end(), |
| 396 | [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; }); |
| 397 | if (it != this->blockingErrors.end()) |
| 398 | { |
| 399 | // Already recorded so just return |
| 400 | logging::log<logging::level::DEBUG>( |
| 401 | "QuiesceOnError set and callout present but entry already logged"); |
| 402 | return; |
| 403 | } |
Andrew Geissler | e4960ee | 2020-03-30 14:31:52 -0500 | [diff] [blame] | 404 | |
Andrew Geissler | e4960ee | 2020-03-30 14:31:52 -0500 | [diff] [blame] | 405 | logging::log<logging::level::INFO>( |
| 406 | "QuiesceOnError set and callout present"); |
Andrew Geissler | 6a0ef6f | 2020-04-06 15:06:31 -0500 | [diff] [blame] | 407 | |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 408 | auto blockPath = |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 409 | std::string(OBJ_LOGGING) + "/block" + std::to_string(entryId); |
| 410 | auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId); |
Andrew Geissler | 6a0ef6f | 2020-04-06 15:06:31 -0500 | [diff] [blame] | 411 | this->blockingErrors.push_back(std::move(blockObj)); |
| 412 | |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 413 | // Register call back if log is resolved |
| 414 | using namespace sdbusplus::bus::match::rules; |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 415 | auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId); |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 416 | auto callback = std::make_unique<sdbusplus::bus::match::match>( |
| 417 | this->busLog, |
| 418 | propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"), |
| 419 | std::bind(std::mem_fn(&Manager::onEntryResolve), this, |
| 420 | std::placeholders::_1)); |
| 421 | |
| 422 | propChangedEntryCallback.insert( |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 423 | std::make_pair(entryId, std::move(callback))); |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 424 | |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 425 | checkAndQuiesceHost(); |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 426 | } |
| 427 | |
Matt Spinler | c64b712 | 2020-03-26 10:55:01 -0500 | [diff] [blame] | 428 | void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc) |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 429 | { |
| 430 | // Make the association <endpointpath>/<endpointtype> paths |
| 431 | std::vector<std::string> assocs; |
| 432 | for (const auto& [forwardType, reverseType, endpoint] : |
| 433 | entry.associations()) |
| 434 | { |
| 435 | std::string e{endpoint}; |
| 436 | e += '/' + reverseType; |
| 437 | assocs.push_back(e); |
| 438 | } |
| 439 | |
| 440 | for (auto& create : Extensions::getCreateFunctions()) |
| 441 | { |
| 442 | try |
| 443 | { |
| 444 | create(entry.message(), entry.id(), entry.timestamp(), |
Matt Spinler | c64b712 | 2020-03-26 10:55:01 -0500 | [diff] [blame] | 445 | entry.severity(), entry.additionalData(), assocs, ffdc); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 446 | } |
| 447 | catch (std::exception& e) |
| 448 | { |
| 449 | log<level::ERR>("An extension's create function threw an exception", |
| 450 | phosphor::logging::entry("ERROR=%s", e.what())); |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
Patrick Williams | f40323d | 2021-04-16 15:35:17 -0500 | [diff] [blame] | 455 | void Manager::processMetadata(const std::string& /*errorName*/, |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 456 | const std::vector<std::string>& additionalData, |
| 457 | AssociationList& objects) const |
| 458 | { |
| 459 | // additionalData is a list of "metadata=value" |
| 460 | constexpr auto separator = '='; |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 461 | for (const auto& entryItem : additionalData) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 462 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 463 | auto found = entryItem.find(separator); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 464 | if (std::string::npos != found) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 465 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 466 | auto metadata = entryItem.substr(0, found); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 467 | auto iter = meta.find(metadata); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 468 | if (meta.end() != iter) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 469 | { |
| 470 | (iter->second)(metadata, additionalData, objects); |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
Andrew Geissler | ced6e2a | 2020-04-07 16:15:29 -0500 | [diff] [blame] | 476 | void Manager::checkAndRemoveBlockingError(uint32_t entryId) |
| 477 | { |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 478 | // First look for blocking object and remove |
Andrew Geissler | ced6e2a | 2020-04-07 16:15:29 -0500 | [diff] [blame] | 479 | auto it = find_if( |
| 480 | blockingErrors.begin(), blockingErrors.end(), |
| 481 | [&](std::unique_ptr<Block>& obj) { return obj->entryId == entryId; }); |
| 482 | if (it != blockingErrors.end()) |
| 483 | { |
| 484 | blockingErrors.erase(it); |
| 485 | } |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 486 | |
| 487 | // Now remove the callback looking for the error to be resolved |
| 488 | auto resolveFind = propChangedEntryCallback.find(entryId); |
| 489 | if (resolveFind != propChangedEntryCallback.end()) |
| 490 | { |
| 491 | propChangedEntryCallback.erase(resolveFind); |
| 492 | } |
| 493 | |
Andrew Geissler | ced6e2a | 2020-04-07 16:15:29 -0500 | [diff] [blame] | 494 | return; |
| 495 | } |
| 496 | |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 497 | void Manager::erase(uint32_t entryId) |
| 498 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 499 | auto entryFound = entries.find(entryId); |
| 500 | if (entries.end() != entryFound) |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 501 | { |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 502 | for (auto& func : Extensions::getDeleteProhibitedFunctions()) |
| 503 | { |
| 504 | try |
| 505 | { |
| 506 | bool prohibited = false; |
| 507 | func(entryId, prohibited); |
| 508 | if (prohibited) |
| 509 | { |
| 510 | // Future work remains to throw an error here. |
| 511 | return; |
| 512 | } |
| 513 | } |
| 514 | catch (std::exception& e) |
| 515 | { |
| 516 | log<level::ERR>( |
| 517 | "An extension's deleteProhibited function threw " |
| 518 | "an exception", |
| 519 | entry("ERROR=%s", e.what())); |
| 520 | } |
| 521 | } |
| 522 | |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 523 | // Delete the persistent representation of this error. |
| 524 | fs::path errorPath(ERRLOG_PERSIST_PATH); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 525 | errorPath /= std::to_string(entryId); |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 526 | fs::remove(errorPath); |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 527 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 528 | auto removeId = [](std::list<uint32_t>& ids, uint32_t id) { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 529 | auto it = std::find(ids.begin(), ids.end(), id); |
| 530 | if (it != ids.end()) |
| 531 | { |
| 532 | ids.erase(it); |
| 533 | } |
| 534 | }; |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 535 | if (entryFound->second->severity() >= Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 536 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 537 | removeId(infoErrors, entryId); |
| 538 | } |
| 539 | else |
| 540 | { |
| 541 | removeId(realErrors, entryId); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 542 | } |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 543 | entries.erase(entryFound); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 544 | |
Andrew Geissler | ced6e2a | 2020-04-07 16:15:29 -0500 | [diff] [blame] | 545 | checkAndRemoveBlockingError(entryId); |
| 546 | |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 547 | for (auto& remove : Extensions::getDeleteFunctions()) |
| 548 | { |
| 549 | try |
| 550 | { |
| 551 | remove(entryId); |
| 552 | } |
| 553 | catch (std::exception& e) |
| 554 | { |
| 555 | log<level::ERR>("An extension's delete function threw an " |
| 556 | "exception", |
| 557 | entry("ERROR=%s", e.what())); |
| 558 | } |
| 559 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 560 | } |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 561 | else |
| 562 | { |
| 563 | logging::log<level::ERR>("Invalid entry ID to delete", |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 564 | logging::entry("ID=%d", entryId)); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 565 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 566 | } |
| 567 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 568 | void Manager::restore() |
| 569 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 570 | auto sanity = [](const auto& id, const auto& restoredId) { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 571 | return id == restoredId; |
| 572 | }; |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 573 | std::vector<uint32_t> errorIds; |
| 574 | |
| 575 | fs::path dir(ERRLOG_PERSIST_PATH); |
| 576 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 577 | { |
| 578 | return; |
| 579 | } |
| 580 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 581 | for (auto& file : fs::directory_iterator(dir)) |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 582 | { |
| 583 | auto id = file.path().filename().c_str(); |
| 584 | auto idNum = std::stol(id); |
| 585 | auto e = std::make_unique<Entry>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 586 | busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this); |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 587 | if (deserialize(file.path(), *e)) |
| 588 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 589 | // validate the restored error entry id |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 590 | if (sanity(static_cast<uint32_t>(idNum), e->id())) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 591 | { |
Adriana Kobylak | 1ff95ef | 2020-12-03 13:52:19 -0600 | [diff] [blame] | 592 | e->path(file.path()); |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 593 | e->emit_object_added(); |
| 594 | if (e->severity() >= Entry::sevLowerLimit) |
| 595 | { |
| 596 | infoErrors.push_back(idNum); |
| 597 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 598 | else |
| 599 | { |
| 600 | realErrors.push_back(idNum); |
| 601 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 602 | |
| 603 | entries.insert(std::make_pair(idNum, std::move(e))); |
| 604 | errorIds.push_back(idNum); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 605 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 606 | else |
| 607 | { |
| 608 | logging::log<logging::level::ERR>( |
| 609 | "Failed in sanity check while restoring error entry. " |
| 610 | "Ignoring error entry", |
| 611 | logging::entry("ID_NUM=%d", idNum), |
| 612 | logging::entry("ENTRY_ID=%d", e->id())); |
| 613 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 617 | if (!errorIds.empty()) |
| 618 | { |
| 619 | entryId = *(std::max_element(errorIds.begin(), errorIds.end())); |
| 620 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 621 | } |
| 622 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 623 | void Manager::journalSync() |
| 624 | { |
| 625 | bool syncRequested = false; |
| 626 | auto fd = -1; |
| 627 | auto rc = -1; |
| 628 | auto wd = -1; |
| 629 | auto bus = sdbusplus::bus::new_default(); |
| 630 | |
| 631 | auto start = |
| 632 | duration_cast<microseconds>(steady_clock::now().time_since_epoch()) |
| 633 | .count(); |
| 634 | |
Matthew Barth | 5991389 | 2019-08-27 15:43:48 -0500 | [diff] [blame] | 635 | // Each time an error log is committed, a request to sync the journal |
| 636 | // must occur and block that error log commit until it completes. A 5sec |
| 637 | // block is done to allow sufficient time for the journal to be synced. |
| 638 | // |
| 639 | // Number of loop iterations = 3 for the following reasons: |
| 640 | // Iteration #1: Requests a journal sync by killing the journald service. |
| 641 | // Iteration #2: Setup an inotify watch to monitor the synced file that |
| 642 | // journald updates with the timestamp the last time the |
| 643 | // journal was flushed. |
| 644 | // Iteration #3: Poll to wait until inotify reports an event which blocks |
| 645 | // the error log from being commited until the sync completes. |
| 646 | constexpr auto maxRetry = 3; |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 647 | for (int i = 0; i < maxRetry; i++) |
| 648 | { |
| 649 | // Read timestamp from synced file |
| 650 | constexpr auto syncedPath = "/run/systemd/journal/synced"; |
| 651 | std::ifstream syncedFile(syncedPath); |
| 652 | if (syncedFile.fail()) |
| 653 | { |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 654 | // If the synced file doesn't exist, a sync request will create it. |
| 655 | if (errno != ENOENT) |
| 656 | { |
| 657 | log<level::ERR>("Failed to open journal synced file", |
| 658 | entry("FILENAME=%s", syncedPath), |
| 659 | entry("ERRNO=%d", errno)); |
| 660 | return; |
| 661 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 662 | } |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 663 | else |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 664 | { |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 665 | // Only read the synced file if it exists. |
| 666 | // See if a sync happened by now |
| 667 | std::string timestampStr; |
| 668 | std::getline(syncedFile, timestampStr); |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 669 | auto timestamp = std::stoll(timestampStr); |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 670 | if (timestamp >= start) |
| 671 | { |
Troy Lee | b7f392a | 2019-11-19 14:34:56 +0800 | [diff] [blame] | 672 | break; |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 673 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | // Let's ask for a sync, but only once |
| 677 | if (!syncRequested) |
| 678 | { |
| 679 | syncRequested = true; |
| 680 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 681 | constexpr auto JOURNAL_UNIT = "systemd-journald.service"; |
| 682 | auto signal = SIGRTMIN + 1; |
| 683 | |
| 684 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 685 | SYSTEMD_INTERFACE, "KillUnit"); |
| 686 | method.append(JOURNAL_UNIT, "main", signal); |
| 687 | bus.call(method); |
| 688 | if (method.is_method_error()) |
| 689 | { |
| 690 | log<level::ERR>("Failed to kill journal service"); |
Troy Lee | b7f392a | 2019-11-19 14:34:56 +0800 | [diff] [blame] | 691 | break; |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 692 | } |
Patrick Williams | a517197 | 2021-04-16 20:10:01 -0500 | [diff] [blame] | 693 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 694 | continue; |
| 695 | } |
| 696 | |
| 697 | // Let's install the inotify watch, if we didn't do that yet. This watch |
| 698 | // monitors the syncedFile for when journald updates it with a newer |
| 699 | // timestamp. This means the journal has been flushed. |
| 700 | if (fd < 0) |
| 701 | { |
| 702 | fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); |
| 703 | if (fd < 0) |
| 704 | { |
| 705 | log<level::ERR>("Failed to create inotify watch", |
| 706 | entry("ERRNO=%d", errno)); |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal"; |
| 711 | wd = inotify_add_watch(fd, JOURNAL_RUN_PATH, |
| 712 | IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR); |
| 713 | if (wd < 0) |
| 714 | { |
| 715 | log<level::ERR>("Failed to watch journal directory", |
| 716 | entry("PATH=%s", JOURNAL_RUN_PATH), |
| 717 | entry("ERRNO=%d", errno)); |
| 718 | close(fd); |
| 719 | return; |
| 720 | } |
| 721 | continue; |
| 722 | } |
| 723 | |
| 724 | // Let's wait until inotify reports an event |
| 725 | struct pollfd fds = { |
Andrew Jeffery | eb0aec4 | 2021-06-15 11:36:04 +0930 | [diff] [blame] | 726 | fd, |
| 727 | POLLIN, |
| 728 | 0, |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 729 | }; |
| 730 | constexpr auto pollTimeout = 5; // 5 seconds |
| 731 | rc = poll(&fds, 1, pollTimeout * 1000); |
| 732 | if (rc < 0) |
| 733 | { |
| 734 | log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno), |
| 735 | entry("ERR=%s", strerror(-rc))); |
| 736 | inotify_rm_watch(fd, wd); |
| 737 | close(fd); |
| 738 | return; |
| 739 | } |
| 740 | else if (rc == 0) |
| 741 | { |
| 742 | log<level::INFO>("Poll timeout, no new journal synced data", |
| 743 | entry("TIMEOUT=%d", pollTimeout)); |
| 744 | break; |
| 745 | } |
| 746 | |
| 747 | // Read from the specified file descriptor until there is no new data, |
| 748 | // throwing away everything read since the timestamp will be read at the |
| 749 | // beginning of the loop. |
| 750 | constexpr auto maxBytes = 64; |
| 751 | uint8_t buffer[maxBytes]; |
| 752 | while (read(fd, buffer, maxBytes) > 0) |
| 753 | ; |
| 754 | } |
| 755 | |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 756 | if (fd != -1) |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 757 | { |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 758 | if (wd != -1) |
| 759 | { |
| 760 | inotify_rm_watch(fd, wd); |
| 761 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 762 | close(fd); |
| 763 | } |
| 764 | |
| 765 | return; |
| 766 | } |
| 767 | |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 768 | std::string Manager::readFWVersion() |
| 769 | { |
Matt Spinler | f61f292 | 2020-06-23 11:32:49 -0500 | [diff] [blame] | 770 | auto version = util::getOSReleaseValue("VERSION_ID"); |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 771 | |
Matt Spinler | f61f292 | 2020-06-23 11:32:49 -0500 | [diff] [blame] | 772 | if (!version) |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 773 | { |
| 774 | log<level::ERR>("Unable to read BMC firmware version"); |
| 775 | } |
| 776 | |
Matt Spinler | f61f292 | 2020-06-23 11:32:49 -0500 | [diff] [blame] | 777 | return version.value_or(""); |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 778 | } |
| 779 | |
Matt Spinler | 3fb83b3 | 2019-07-26 11:22:44 -0500 | [diff] [blame] | 780 | void Manager::create(const std::string& message, Entry::Level severity, |
| 781 | const std::map<std::string, std::string>& additionalData) |
| 782 | { |
| 783 | // Convert the map into a vector of "key=value" strings |
| 784 | std::vector<std::string> ad; |
| 785 | metadata::associations::combine(additionalData, ad); |
| 786 | |
| 787 | createEntry(message, severity, ad); |
| 788 | } |
| 789 | |
Matt Spinler | c64b712 | 2020-03-26 10:55:01 -0500 | [diff] [blame] | 790 | void Manager::createWithFFDC( |
| 791 | const std::string& message, Entry::Level severity, |
| 792 | const std::map<std::string, std::string>& additionalData, |
| 793 | const FFDCEntries& ffdc) |
| 794 | { |
| 795 | // Convert the map into a vector of "key=value" strings |
| 796 | std::vector<std::string> ad; |
| 797 | metadata::associations::combine(additionalData, ad); |
| 798 | |
| 799 | createEntry(message, severity, ad, ffdc); |
| 800 | } |
| 801 | |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 802 | } // namespace internal |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 803 | } // namespace logging |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 804 | } // namespace phosphor |