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 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 17 | #include <phosphor-logging/lg2.hpp> |
| 18 | #include <sdbusplus/vtable.hpp> |
| 19 | #include <xyz/openbmc_project/State/Host/server.hpp> |
| 20 | |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 21 | #include <cassert> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 22 | #include <chrono> |
| 23 | #include <cstdio> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 24 | #include <cstring> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 25 | #include <fstream> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 26 | #include <functional> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 27 | #include <future> |
| 28 | #include <iostream> |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 29 | #include <map> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 30 | #include <set> |
| 31 | #include <string> |
Patrick Williams | b01a5b4 | 2021-08-28 15:11:45 -0500 | [diff] [blame] | 32 | #include <string_view> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 33 | #include <vector> |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 34 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 35 | using namespace std::chrono; |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 36 | extern const std::map< |
| 37 | phosphor::logging::metadata::Metadata, |
| 38 | std::function<phosphor::logging::metadata::associations::Type>> |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 39 | meta; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 40 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 41 | namespace phosphor |
| 42 | { |
| 43 | namespace logging |
| 44 | { |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 45 | namespace internal |
| 46 | { |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 47 | inline auto getLevel(const std::string& errMsg) |
| 48 | { |
| 49 | auto reqLevel = Entry::Level::Error; // Default to Error |
| 50 | |
| 51 | auto levelmap = g_errLevelMap.find(errMsg); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 52 | if (levelmap != g_errLevelMap.end()) |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 53 | { |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 54 | reqLevel = static_cast<Entry::Level>(levelmap->second); |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 57 | return reqLevel; |
| 58 | } |
| 59 | |
Nagaraju Goruganti | 477b731 | 2018-06-25 23:28:58 -0500 | [diff] [blame] | 60 | int Manager::getRealErrSize() |
| 61 | { |
| 62 | return realErrors.size(); |
| 63 | } |
| 64 | |
| 65 | int Manager::getInfoErrSize() |
| 66 | { |
| 67 | return infoErrors.size(); |
| 68 | } |
| 69 | |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 70 | uint32_t Manager::commit(uint64_t transactionId, std::string errMsg) |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 71 | { |
| 72 | auto level = getLevel(errMsg); |
| 73 | _commit(transactionId, std::move(errMsg), level); |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 74 | return entryId; |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 77 | uint32_t Manager::commitWithLvl(uint64_t transactionId, std::string errMsg, |
| 78 | uint32_t errLvl) |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 79 | { |
| 80 | _commit(transactionId, std::move(errMsg), |
| 81 | static_cast<Entry::Level>(errLvl)); |
Lei YU | b50c705 | 2021-01-21 16:02:26 +0800 | [diff] [blame] | 82 | return entryId; |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 83 | } |
| 84 | |
Patrick Williams | a517197 | 2021-04-16 20:10:01 -0500 | [diff] [blame] | 85 | void Manager::_commit(uint64_t transactionId [[maybe_unused]], |
| 86 | std::string&& errMsg, Entry::Level errLvl) |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 87 | { |
Patrick Williams | a517197 | 2021-04-16 20:10:01 -0500 | [diff] [blame] | 88 | std::vector<std::string> additionalData{}; |
| 89 | |
| 90 | // When running as a test-case, the system may have a LOT of journal |
| 91 | // data and we may not have permissions to do some of the journal sync |
| 92 | // operations. Just skip over them. |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 93 | if (!IS_UNIT_TEST) |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 94 | { |
Patrick Williams | b01a5b4 | 2021-08-28 15:11:45 -0500 | [diff] [blame] | 95 | static constexpr auto transactionIdVar = |
| 96 | std::string_view{"TRANSACTION_ID"}; |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 97 | // Length of 'TRANSACTION_ID' string. |
Patrick Williams | b01a5b4 | 2021-08-28 15:11:45 -0500 | [diff] [blame] | 98 | static constexpr auto transactionIdVarSize = transactionIdVar.size(); |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 99 | // Length of 'TRANSACTION_ID=' string. |
Patrick Williams | b01a5b4 | 2021-08-28 15:11:45 -0500 | [diff] [blame] | 100 | static constexpr auto transactionIdVarOffset = transactionIdVarSize + 1; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 101 | |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 102 | // Flush all the pending log messages into the journal |
| 103 | journalSync(); |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 104 | |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 105 | sd_journal* j = nullptr; |
| 106 | int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 107 | if (rc < 0) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 108 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 109 | lg2::error("Failed to open journal: {ERROR}", "ERROR", |
| 110 | strerror(-rc)); |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 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 |
Patrick Williams | b01a5b4 | 2021-08-28 15:11:45 -0500 | [diff] [blame] | 134 | rc = sd_journal_get_data(j, transactionIdVar.data(), |
| 135 | (const void**)&data, &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 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 189 | lg2::info("Failed to find metadata: {META_FIELD}", "META_FIELD", |
| 190 | metaVarStr); |
William A. Kennington III | b6b2557 | 2021-05-19 17:09:41 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | sd_journal_close(j); |
| 195 | } |
Matt Spinler | b60e755 | 2019-07-24 15:28:08 -0500 | [diff] [blame] | 196 | createEntry(errMsg, errLvl, additionalData); |
| 197 | } |
| 198 | |
| 199 | void Manager::createEntry(std::string errMsg, Entry::Level errLvl, |
Matt Spinler | c64b712 | 2020-03-26 10:55:01 -0500 | [diff] [blame] | 200 | std::vector<std::string> additionalData, |
| 201 | const FFDCEntries& ffdc) |
Matt Spinler | b60e755 | 2019-07-24 15:28:08 -0500 | [diff] [blame] | 202 | { |
| 203 | if (!Extensions::disableDefaultLogCaps()) |
| 204 | { |
| 205 | if (errLvl < Entry::sevLowerLimit) |
| 206 | { |
| 207 | if (realErrors.size() >= ERROR_CAP) |
| 208 | { |
| 209 | erase(realErrors.front()); |
| 210 | } |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | if (infoErrors.size() >= ERROR_INFO_CAP) |
| 215 | { |
| 216 | erase(infoErrors.front()); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 221 | entryId++; |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 222 | if (errLvl >= Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 223 | { |
| 224 | infoErrors.push_back(entryId); |
| 225 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 226 | else |
| 227 | { |
| 228 | realErrors.push_back(entryId); |
| 229 | } |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 230 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 231 | std::chrono::system_clock::now().time_since_epoch()) |
| 232 | .count(); |
| 233 | auto objPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 234 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 235 | AssociationList objects{}; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 236 | processMetadata(errMsg, additionalData, objects); |
| 237 | |
Matt Spinler | fb978da | 2022-01-21 08:42:24 -0600 | [diff] [blame] | 238 | auto e = std::make_unique<Entry>( |
| 239 | busLog, objPath, entryId, |
| 240 | ms, // Milliseconds since 1970 |
| 241 | errLvl, std::move(errMsg), std::move(additionalData), |
| 242 | std::move(objects), fwVersion, getEntrySerializePath(entryId), *this); |
| 243 | |
| 244 | serialize(*e); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 245 | |
Matt Spinler | 601b811 | 2022-01-26 13:18:04 -0600 | [diff] [blame] | 246 | if (isQuiesceOnErrorEnabled() && (errLvl < Entry::sevLowerLimit) && |
| 247 | 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 | } |
Patrick Williams | 45e8352 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 282 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 283 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 284 | lg2::error("Error reading QuiesceOnHwError property: {ERROR}", "ERROR", |
| 285 | e); |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 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 | |
Patrick Williams | 45e8352 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 316 | void Manager::onEntryResolve(sdbusplus::message_t& msg) |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 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 | { |
Patrick Williams | a144a27 | 2021-07-16 17:03:55 -0500 | [diff] [blame] | 340 | using Host = sdbusplus::xyz::openbmc_project::State::server::Host; |
| 341 | |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 342 | // First check host state |
Patrick Williams | a144a27 | 2021-07-16 17:03:55 -0500 | [diff] [blame] | 343 | std::variant<Host::HostState> property; |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 344 | |
| 345 | auto method = this->busLog.new_method_call( |
| 346 | "xyz.openbmc_project.State.Host", "/xyz/openbmc_project/state/host0", |
| 347 | "org.freedesktop.DBus.Properties", "Get"); |
| 348 | |
| 349 | method.append("xyz.openbmc_project.State.Host", "CurrentHostState"); |
| 350 | |
| 351 | try |
| 352 | { |
| 353 | auto reply = this->busLog.call(method); |
| 354 | reply.read(property); |
| 355 | } |
Patrick Williams | 45e8352 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 356 | catch (const sdbusplus::exception_t& e) |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 357 | { |
| 358 | // Quiescing the host is a "best effort" type function. If unable to |
| 359 | // read the host state or it comes back empty, just return. |
| 360 | // The boot block object will still be created and the associations to |
| 361 | // find the log will be present. Don't want a dependency with |
| 362 | // phosphor-state-manager service |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 363 | lg2::info("Error reading QuiesceOnHwError property: {ERROR}", "ERROR", |
| 364 | e); |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 365 | return; |
| 366 | } |
| 367 | |
Patrick Williams | a144a27 | 2021-07-16 17:03:55 -0500 | [diff] [blame] | 368 | auto hostState = std::get<Host::HostState>(property); |
| 369 | if (hostState != Host::HostState::Running) |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 370 | { |
| 371 | return; |
| 372 | } |
| 373 | |
| 374 | auto quiesce = this->busLog.new_method_call( |
| 375 | "org.freedesktop.systemd1", "/org/freedesktop/systemd1", |
| 376 | "org.freedesktop.systemd1.Manager", "StartUnit"); |
| 377 | |
Andrew Geissler | 86a60e9 | 2022-04-15 14:36:02 -0500 | [diff] [blame] | 378 | quiesce.append("obmc-host-graceful-quiesce@0.target"); |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 379 | quiesce.append("replace"); |
| 380 | |
| 381 | this->busLog.call_noreply(quiesce); |
| 382 | } |
| 383 | |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 384 | void Manager::quiesceOnError(const uint32_t entryId) |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 385 | { |
Andrew Geissler | 72a1cca | 2020-09-10 16:49:09 -0500 | [diff] [blame] | 386 | // Verify we don't already have this entry blocking |
Matt Spinler | 4a37595 | 2022-07-01 11:15:33 -0500 | [diff] [blame] | 387 | auto it = find_if(this->blockingErrors.begin(), this->blockingErrors.end(), |
| 388 | [&](const std::unique_ptr<Block>& obj) { |
| 389 | return obj->entryId == entryId; |
| 390 | }); |
Andrew Geissler | 72a1cca | 2020-09-10 16:49:09 -0500 | [diff] [blame] | 391 | if (it != this->blockingErrors.end()) |
| 392 | { |
| 393 | // Already recorded so just return |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 394 | lg2::debug( |
Andrew Geissler | 72a1cca | 2020-09-10 16:49:09 -0500 | [diff] [blame] | 395 | "QuiesceOnError set and callout present but entry already logged"); |
| 396 | return; |
| 397 | } |
Andrew Geissler | e4960ee | 2020-03-30 14:31:52 -0500 | [diff] [blame] | 398 | |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 399 | lg2::info("QuiesceOnError set and callout present"); |
Andrew Geissler | 6a0ef6f | 2020-04-06 15:06:31 -0500 | [diff] [blame] | 400 | |
Patrick Williams | 2544b41 | 2022-10-04 08:41:06 -0500 | [diff] [blame] | 401 | auto blockPath = std::string(OBJ_LOGGING) + "/block" + |
| 402 | std::to_string(entryId); |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 403 | auto blockObj = std::make_unique<Block>(this->busLog, blockPath, entryId); |
Andrew Geissler | 6a0ef6f | 2020-04-06 15:06:31 -0500 | [diff] [blame] | 404 | this->blockingErrors.push_back(std::move(blockObj)); |
| 405 | |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 406 | // Register call back if log is resolved |
| 407 | using namespace sdbusplus::bus::match::rules; |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 408 | auto entryPath = std::string(OBJ_ENTRY) + '/' + std::to_string(entryId); |
Patrick Williams | 45e8352 | 2022-07-22 19:26:52 -0500 | [diff] [blame] | 409 | auto callback = std::make_unique<sdbusplus::bus::match_t>( |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 410 | this->busLog, |
| 411 | propertiesChanged(entryPath, "xyz.openbmc_project.Logging.Entry"), |
| 412 | std::bind(std::mem_fn(&Manager::onEntryResolve), this, |
| 413 | std::placeholders::_1)); |
| 414 | |
| 415 | propChangedEntryCallback.insert( |
Andrew Geissler | 3287454 | 2020-07-09 09:17:03 -0500 | [diff] [blame] | 416 | std::make_pair(entryId, std::move(callback))); |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 417 | |
Andrew Geissler | f6126a7 | 2020-05-08 10:41:09 -0500 | [diff] [blame] | 418 | checkAndQuiesceHost(); |
Andrew Geissler | c0c500e | 2020-03-26 11:08:56 -0500 | [diff] [blame] | 419 | } |
| 420 | |
Matt Spinler | c64b712 | 2020-03-26 10:55:01 -0500 | [diff] [blame] | 421 | void Manager::doExtensionLogCreate(const Entry& entry, const FFDCEntries& ffdc) |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 422 | { |
| 423 | // Make the association <endpointpath>/<endpointtype> paths |
| 424 | std::vector<std::string> assocs; |
| 425 | for (const auto& [forwardType, reverseType, endpoint] : |
| 426 | entry.associations()) |
| 427 | { |
| 428 | std::string e{endpoint}; |
| 429 | e += '/' + reverseType; |
| 430 | assocs.push_back(e); |
| 431 | } |
| 432 | |
| 433 | for (auto& create : Extensions::getCreateFunctions()) |
| 434 | { |
| 435 | try |
| 436 | { |
| 437 | create(entry.message(), entry.id(), entry.timestamp(), |
Matt Spinler | c64b712 | 2020-03-26 10:55:01 -0500 | [diff] [blame] | 438 | entry.severity(), entry.additionalData(), assocs, ffdc); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 439 | } |
Patrick Williams | 66491c6 | 2021-10-06 12:23:37 -0500 | [diff] [blame] | 440 | catch (const std::exception& e) |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 441 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 442 | lg2::error( |
| 443 | "An extension's create function threw an exception: {ERROR}", |
| 444 | "ERROR", e); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Patrick Williams | f40323d | 2021-04-16 15:35:17 -0500 | [diff] [blame] | 449 | void Manager::processMetadata(const std::string& /*errorName*/, |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 450 | const std::vector<std::string>& additionalData, |
| 451 | AssociationList& objects) const |
| 452 | { |
| 453 | // additionalData is a list of "metadata=value" |
| 454 | constexpr auto separator = '='; |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 455 | for (const auto& entryItem : additionalData) |
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 found = entryItem.find(separator); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 458 | if (std::string::npos != found) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 459 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 460 | auto metadata = entryItem.substr(0, found); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 461 | auto iter = meta.find(metadata); |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 462 | if (meta.end() != iter) |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 463 | { |
| 464 | (iter->second)(metadata, additionalData, objects); |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | |
Andrew Geissler | ced6e2a | 2020-04-07 16:15:29 -0500 | [diff] [blame] | 470 | void Manager::checkAndRemoveBlockingError(uint32_t entryId) |
| 471 | { |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 472 | // First look for blocking object and remove |
Matt Spinler | 4a37595 | 2022-07-01 11:15:33 -0500 | [diff] [blame] | 473 | auto it = find_if(blockingErrors.begin(), blockingErrors.end(), |
| 474 | [&](const std::unique_ptr<Block>& obj) { |
| 475 | return obj->entryId == entryId; |
| 476 | }); |
Andrew Geissler | ced6e2a | 2020-04-07 16:15:29 -0500 | [diff] [blame] | 477 | if (it != blockingErrors.end()) |
| 478 | { |
| 479 | blockingErrors.erase(it); |
| 480 | } |
Andrew Geissler | 7f6d4bc | 2020-04-16 14:47:34 -0500 | [diff] [blame] | 481 | |
| 482 | // Now remove the callback looking for the error to be resolved |
| 483 | auto resolveFind = propChangedEntryCallback.find(entryId); |
| 484 | if (resolveFind != propChangedEntryCallback.end()) |
| 485 | { |
| 486 | propChangedEntryCallback.erase(resolveFind); |
| 487 | } |
| 488 | |
Andrew Geissler | ced6e2a | 2020-04-07 16:15:29 -0500 | [diff] [blame] | 489 | return; |
| 490 | } |
| 491 | |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 492 | void Manager::erase(uint32_t entryId) |
| 493 | { |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 494 | auto entryFound = entries.find(entryId); |
| 495 | if (entries.end() != entryFound) |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 496 | { |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 497 | for (auto& func : Extensions::getDeleteProhibitedFunctions()) |
| 498 | { |
| 499 | try |
| 500 | { |
| 501 | bool prohibited = false; |
| 502 | func(entryId, prohibited); |
| 503 | if (prohibited) |
| 504 | { |
| 505 | // Future work remains to throw an error here. |
| 506 | return; |
| 507 | } |
| 508 | } |
Patrick Williams | 66491c6 | 2021-10-06 12:23:37 -0500 | [diff] [blame] | 509 | catch (const std::exception& e) |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 510 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 511 | lg2::error("An extension's deleteProhibited function threw an " |
| 512 | "exception: {ERROR}", |
| 513 | "ERROR", e); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 514 | } |
| 515 | } |
| 516 | |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 517 | // Delete the persistent representation of this error. |
| 518 | fs::path errorPath(ERRLOG_PERSIST_PATH); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 519 | errorPath /= std::to_string(entryId); |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 520 | fs::remove(errorPath); |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 521 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 522 | auto removeId = [](std::list<uint32_t>& ids, uint32_t id) { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 523 | auto it = std::find(ids.begin(), ids.end(), id); |
| 524 | if (it != ids.end()) |
| 525 | { |
| 526 | ids.erase(it); |
| 527 | } |
| 528 | }; |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 529 | if (entryFound->second->severity() >= Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 530 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 531 | removeId(infoErrors, entryId); |
| 532 | } |
| 533 | else |
| 534 | { |
| 535 | removeId(realErrors, entryId); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 536 | } |
Patrick Venture | 3443896 | 2018-10-30 13:17:37 -0700 | [diff] [blame] | 537 | entries.erase(entryFound); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 538 | |
Andrew Geissler | ced6e2a | 2020-04-07 16:15:29 -0500 | [diff] [blame] | 539 | checkAndRemoveBlockingError(entryId); |
| 540 | |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 541 | for (auto& remove : Extensions::getDeleteFunctions()) |
| 542 | { |
| 543 | try |
| 544 | { |
| 545 | remove(entryId); |
| 546 | } |
Patrick Williams | 66491c6 | 2021-10-06 12:23:37 -0500 | [diff] [blame] | 547 | catch (const std::exception& e) |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 548 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 549 | lg2::error("An extension's delete function threw an exception: " |
| 550 | "{ERROR}", |
| 551 | "ERROR", e); |
Matt Spinler | 99c2b40 | 2019-05-23 14:29:16 -0500 | [diff] [blame] | 552 | } |
| 553 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 554 | } |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 555 | else |
| 556 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 557 | lg2::error("Invalid entry ID ({ID}) to delete", "ID", entryId); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 558 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 559 | } |
| 560 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 561 | void Manager::restore() |
| 562 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 563 | auto sanity = [](const auto& id, const auto& restoredId) { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 564 | return id == restoredId; |
| 565 | }; |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 566 | |
| 567 | fs::path dir(ERRLOG_PERSIST_PATH); |
| 568 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 569 | { |
| 570 | return; |
| 571 | } |
| 572 | |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 573 | for (auto& file : fs::directory_iterator(dir)) |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 574 | { |
| 575 | auto id = file.path().filename().c_str(); |
| 576 | auto idNum = std::stol(id); |
| 577 | auto e = std::make_unique<Entry>( |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 578 | busLog, std::string(OBJ_ENTRY) + '/' + id, idNum, *this); |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 579 | if (deserialize(file.path(), *e)) |
| 580 | { |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 581 | // validate the restored error entry id |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 582 | if (sanity(static_cast<uint32_t>(idNum), e->id())) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 583 | { |
Matt Spinler | ef952af | 2021-08-19 10:23:05 -0500 | [diff] [blame] | 584 | e->path(file.path(), true); |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 585 | if (e->severity() >= Entry::sevLowerLimit) |
| 586 | { |
| 587 | infoErrors.push_back(idNum); |
| 588 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 589 | else |
| 590 | { |
| 591 | realErrors.push_back(idNum); |
| 592 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 593 | |
| 594 | entries.insert(std::make_pair(idNum, std::move(e))); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 595 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 596 | else |
| 597 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 598 | lg2::error( |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 599 | "Failed in sanity check while restoring error entry. " |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 600 | "Ignoring error entry {ID_NUM}/{ENTRY_ID}.", |
| 601 | "ID_NUM", idNum, "ENTRY_ID", e->id()); |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 602 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Lei YU | 01bf5c4 | 2022-05-23 15:33:23 +0800 | [diff] [blame] | 606 | if (!entries.empty()) |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 607 | { |
Lei YU | 01bf5c4 | 2022-05-23 15:33:23 +0800 | [diff] [blame] | 608 | entryId = entries.rbegin()->first; |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 609 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 610 | } |
| 611 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 612 | void Manager::journalSync() |
| 613 | { |
| 614 | bool syncRequested = false; |
| 615 | auto fd = -1; |
| 616 | auto rc = -1; |
| 617 | auto wd = -1; |
| 618 | auto bus = sdbusplus::bus::new_default(); |
| 619 | |
| 620 | auto start = |
| 621 | duration_cast<microseconds>(steady_clock::now().time_since_epoch()) |
| 622 | .count(); |
| 623 | |
Matthew Barth | 5991389 | 2019-08-27 15:43:48 -0500 | [diff] [blame] | 624 | // Each time an error log is committed, a request to sync the journal |
| 625 | // must occur and block that error log commit until it completes. A 5sec |
| 626 | // block is done to allow sufficient time for the journal to be synced. |
| 627 | // |
| 628 | // Number of loop iterations = 3 for the following reasons: |
| 629 | // Iteration #1: Requests a journal sync by killing the journald service. |
| 630 | // Iteration #2: Setup an inotify watch to monitor the synced file that |
| 631 | // journald updates with the timestamp the last time the |
| 632 | // journal was flushed. |
| 633 | // Iteration #3: Poll to wait until inotify reports an event which blocks |
| 634 | // the error log from being commited until the sync completes. |
| 635 | constexpr auto maxRetry = 3; |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 636 | for (int i = 0; i < maxRetry; i++) |
| 637 | { |
| 638 | // Read timestamp from synced file |
| 639 | constexpr auto syncedPath = "/run/systemd/journal/synced"; |
| 640 | std::ifstream syncedFile(syncedPath); |
| 641 | if (syncedFile.fail()) |
| 642 | { |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 643 | // If the synced file doesn't exist, a sync request will create it. |
| 644 | if (errno != ENOENT) |
| 645 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 646 | lg2::error( |
| 647 | "Failed to open journal synced file {FILENAME}: {ERROR}", |
| 648 | "FILENAME", syncedPath, "ERROR", strerror(errno)); |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 649 | return; |
| 650 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 651 | } |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 652 | else |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 653 | { |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 654 | // Only read the synced file if it exists. |
| 655 | // See if a sync happened by now |
| 656 | std::string timestampStr; |
| 657 | std::getline(syncedFile, timestampStr); |
Patrick Venture | 30047bf | 2018-11-01 18:52:15 -0700 | [diff] [blame] | 658 | auto timestamp = std::stoll(timestampStr); |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 659 | if (timestamp >= start) |
| 660 | { |
Troy Lee | b7f392a | 2019-11-19 14:34:56 +0800 | [diff] [blame] | 661 | break; |
Adriana Kobylak | 7d111a8 | 2018-09-04 10:14:24 -0500 | [diff] [blame] | 662 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | // Let's ask for a sync, but only once |
| 666 | if (!syncRequested) |
| 667 | { |
| 668 | syncRequested = true; |
| 669 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 670 | constexpr auto JOURNAL_UNIT = "systemd-journald.service"; |
| 671 | auto signal = SIGRTMIN + 1; |
| 672 | |
| 673 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 674 | SYSTEMD_INTERFACE, "KillUnit"); |
| 675 | method.append(JOURNAL_UNIT, "main", signal); |
| 676 | bus.call(method); |
| 677 | if (method.is_method_error()) |
| 678 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 679 | lg2::error("Failed to kill journal service"); |
Troy Lee | b7f392a | 2019-11-19 14:34:56 +0800 | [diff] [blame] | 680 | break; |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 681 | } |
Patrick Williams | a517197 | 2021-04-16 20:10:01 -0500 | [diff] [blame] | 682 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 683 | continue; |
| 684 | } |
| 685 | |
| 686 | // Let's install the inotify watch, if we didn't do that yet. This watch |
| 687 | // monitors the syncedFile for when journald updates it with a newer |
| 688 | // timestamp. This means the journal has been flushed. |
| 689 | if (fd < 0) |
| 690 | { |
| 691 | fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); |
| 692 | if (fd < 0) |
| 693 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 694 | lg2::error("Failed to create inotify watch: {ERROR}", "ERROR", |
| 695 | strerror(errno)); |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 696 | return; |
| 697 | } |
| 698 | |
| 699 | constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal"; |
| 700 | wd = inotify_add_watch(fd, JOURNAL_RUN_PATH, |
| 701 | IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR); |
| 702 | if (wd < 0) |
| 703 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 704 | lg2::error("Failed to watch journal directory: {PATH}: {ERROR}", |
| 705 | "PATH", JOURNAL_RUN_PATH, "ERROR", strerror(errno)); |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 706 | close(fd); |
| 707 | return; |
| 708 | } |
| 709 | continue; |
| 710 | } |
| 711 | |
| 712 | // Let's wait until inotify reports an event |
| 713 | struct pollfd fds = { |
Andrew Jeffery | eb0aec4 | 2021-06-15 11:36:04 +0930 | [diff] [blame] | 714 | fd, |
| 715 | POLLIN, |
| 716 | 0, |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 717 | }; |
| 718 | constexpr auto pollTimeout = 5; // 5 seconds |
| 719 | rc = poll(&fds, 1, pollTimeout * 1000); |
| 720 | if (rc < 0) |
| 721 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 722 | lg2::error("Failed to add event: {ERROR}", "ERROR", |
| 723 | strerror(errno)); |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 724 | inotify_rm_watch(fd, wd); |
| 725 | close(fd); |
| 726 | return; |
| 727 | } |
| 728 | else if (rc == 0) |
| 729 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 730 | lg2::info("Poll timeout ({TIMEOUT}), no new journal synced data", |
| 731 | "TIMEOUT", pollTimeout); |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | |
| 735 | // Read from the specified file descriptor until there is no new data, |
| 736 | // throwing away everything read since the timestamp will be read at the |
| 737 | // beginning of the loop. |
| 738 | constexpr auto maxBytes = 64; |
| 739 | uint8_t buffer[maxBytes]; |
| 740 | while (read(fd, buffer, maxBytes) > 0) |
| 741 | ; |
| 742 | } |
| 743 | |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 744 | if (fd != -1) |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 745 | { |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 746 | if (wd != -1) |
| 747 | { |
| 748 | inotify_rm_watch(fd, wd); |
| 749 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 750 | close(fd); |
| 751 | } |
| 752 | |
| 753 | return; |
| 754 | } |
| 755 | |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 756 | std::string Manager::readFWVersion() |
| 757 | { |
Matt Spinler | f61f292 | 2020-06-23 11:32:49 -0500 | [diff] [blame] | 758 | auto version = util::getOSReleaseValue("VERSION_ID"); |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 759 | |
Matt Spinler | f61f292 | 2020-06-23 11:32:49 -0500 | [diff] [blame] | 760 | if (!version) |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 761 | { |
Patrick Williams | 5f285c5 | 2021-07-27 21:25:38 -0500 | [diff] [blame] | 762 | lg2::error("Unable to read BMC firmware version"); |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 763 | } |
| 764 | |
Matt Spinler | f61f292 | 2020-06-23 11:32:49 -0500 | [diff] [blame] | 765 | return version.value_or(""); |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 766 | } |
| 767 | |
Matt Spinler | 3fb83b3 | 2019-07-26 11:22:44 -0500 | [diff] [blame] | 768 | void Manager::create(const std::string& message, Entry::Level severity, |
| 769 | const std::map<std::string, std::string>& additionalData) |
| 770 | { |
| 771 | // Convert the map into a vector of "key=value" strings |
| 772 | std::vector<std::string> ad; |
| 773 | metadata::associations::combine(additionalData, ad); |
| 774 | |
| 775 | createEntry(message, severity, ad); |
| 776 | } |
| 777 | |
Matt Spinler | c64b712 | 2020-03-26 10:55:01 -0500 | [diff] [blame] | 778 | void Manager::createWithFFDC( |
| 779 | const std::string& message, Entry::Level severity, |
| 780 | const std::map<std::string, std::string>& additionalData, |
| 781 | const FFDCEntries& ffdc) |
| 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, ffdc); |
| 788 | } |
| 789 | |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 790 | } // namespace internal |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 791 | } // namespace logging |
Patrick Venture | f18bf83 | 2018-10-26 18:14:00 -0700 | [diff] [blame] | 792 | } // namespace phosphor |