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