Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 1 | #include <fstream> |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 2 | #include <future> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 3 | #include <iostream> |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 4 | #include <chrono> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 5 | #include <cstdio> |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 6 | #include <poll.h> |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 7 | #include <set> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 10 | #include <sdbusplus/vtable.hpp> |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 11 | #include <sys/inotify.h> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 12 | #include <systemd/sd-bus.h> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 13 | #include <systemd/sd-journal.h> |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 14 | #include <unistd.h> |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 15 | #include "config.h" |
| 16 | #include "elog_entry.hpp" |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 17 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 18 | #include "log_manager.hpp" |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 19 | #include "elog_meta.hpp" |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 20 | #include "elog_serialize.hpp" |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 21 | |
| 22 | using namespace phosphor::logging; |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 23 | using namespace std::chrono; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 24 | extern const std::map<metadata::Metadata, |
| 25 | std::function<metadata::associations::Type>> meta; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 26 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 27 | namespace phosphor |
| 28 | { |
| 29 | namespace logging |
| 30 | { |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 31 | namespace internal |
| 32 | { |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 33 | inline auto getLevel(const std::string& errMsg) |
| 34 | { |
| 35 | auto reqLevel = Entry::Level::Error; // Default to Error |
| 36 | |
| 37 | auto levelmap = g_errLevelMap.find(errMsg); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 38 | if (levelmap != g_errLevelMap.end()) |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 39 | { |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 40 | reqLevel = static_cast<Entry::Level>(levelmap->second); |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 41 | } |
| 42 | |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 43 | return reqLevel; |
| 44 | } |
| 45 | |
Nagaraju Goruganti | 477b731 | 2018-06-25 23:28:58 -0500 | [diff] [blame] | 46 | int Manager::getRealErrSize() |
| 47 | { |
| 48 | return realErrors.size(); |
| 49 | } |
| 50 | |
| 51 | int Manager::getInfoErrSize() |
| 52 | { |
| 53 | return infoErrors.size(); |
| 54 | } |
| 55 | |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 56 | void Manager::commit(uint64_t transactionId, std::string errMsg) |
| 57 | { |
| 58 | auto level = getLevel(errMsg); |
| 59 | _commit(transactionId, std::move(errMsg), level); |
| 60 | } |
| 61 | |
| 62 | void Manager::commitWithLvl(uint64_t transactionId, std::string errMsg, |
| 63 | uint32_t errLvl) |
| 64 | { |
| 65 | _commit(transactionId, std::move(errMsg), |
| 66 | static_cast<Entry::Level>(errLvl)); |
| 67 | } |
| 68 | |
| 69 | void Manager::_commit(uint64_t transactionId, std::string&& errMsg, |
| 70 | Entry::Level errLvl) |
| 71 | { |
| 72 | if (errLvl < Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 73 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 74 | if (realErrors.size() >= ERROR_CAP) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 75 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 76 | erase(realErrors.front()); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | else |
| 80 | { |
| 81 | if (infoErrors.size() >= ERROR_INFO_CAP) |
| 82 | { |
| 83 | erase(infoErrors.front()); |
| 84 | } |
| 85 | } |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 86 | constexpr const auto transactionIdVar = "TRANSACTION_ID"; |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 87 | // Length of 'TRANSACTION_ID' string. |
Adriana Kobylak | 6721899 | 2017-02-28 12:53:37 -0600 | [diff] [blame] | 88 | constexpr const auto transactionIdVarSize = strlen(transactionIdVar); |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 89 | // Length of 'TRANSACTION_ID=' string. |
| 90 | constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 91 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 92 | // Flush all the pending log messages into the journal |
| 93 | journalSync(); |
Adriana Kobylak | cfd9a7d | 2017-06-07 11:57:31 -0500 | [diff] [blame] | 94 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 95 | sd_journal *j = nullptr; |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 96 | int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 97 | if (rc < 0) |
| 98 | { |
| 99 | logging::log<logging::level::ERR>("Failed to open journal", |
| 100 | logging::entry("DESCRIPTION=%s", strerror(-rc))); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 101 | return; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 104 | std::string transactionIdStr = std::to_string(transactionId); |
Adriana Kobylak | d722b3a | 2017-02-28 12:10:44 -0600 | [diff] [blame] | 105 | std::set<std::string> metalist; |
| 106 | auto metamap = g_errMetaMap.find(errMsg); |
| 107 | if (metamap != g_errMetaMap.end()) |
| 108 | { |
| 109 | metalist.insert(metamap->second.begin(), metamap->second.end()); |
| 110 | } |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 111 | |
Jayanth Othayoth | db18ebe | 2017-09-04 00:48:02 -0500 | [diff] [blame] | 112 | //Add _PID field information in AdditionalData. |
| 113 | metalist.insert("_PID"); |
| 114 | |
Tom Joseph | 7a33ee4 | 2017-07-25 00:04:20 +0530 | [diff] [blame] | 115 | std::vector<std::string> additionalData; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 116 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 117 | // Read the journal from the end to get the most recent entry first. |
| 118 | // The result from the sd_journal_get_data() is of the form VARIABLE=value. |
| 119 | SD_JOURNAL_FOREACH_BACKWARDS(j) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 120 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 121 | const char *data = nullptr; |
| 122 | size_t length = 0; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 123 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 124 | // Look for the transaction id metadata variable |
| 125 | rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data, |
| 126 | &length); |
| 127 | if (rc < 0) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 128 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 129 | // This journal entry does not have the TRANSACTION_ID |
| 130 | // metadata variable. |
| 131 | continue; |
| 132 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 133 | |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 134 | // journald does not guarantee that sd_journal_get_data() returns NULL |
| 135 | // terminated strings, so need to specify the size to use to compare, |
| 136 | // use the returned length instead of anything that relies on NULL |
| 137 | // terminators like strlen(). |
| 138 | // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove |
| 139 | // the TRANSACTION_ID characters plus the (=) sign to do the comparison. |
| 140 | // 'data + transactionIdVarOffset' will be in the form of '1234'. |
| 141 | // 'length - transactionIdVarOffset' will be the length of '1234'. |
| 142 | if ((length <= (transactionIdVarOffset)) || |
| 143 | (transactionIdStr.compare(0, |
| 144 | transactionIdStr.size(), |
| 145 | data + transactionIdVarOffset, |
| 146 | length - transactionIdVarOffset) != 0)) |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 147 | { |
| 148 | // The value of the TRANSACTION_ID metadata is not the requested |
| 149 | // transaction id number. |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | // Search for all metadata variables in the current journal entry. |
| 154 | for (auto i = metalist.cbegin(); i != metalist.cend();) |
| 155 | { |
| 156 | rc = sd_journal_get_data(j, (*i).c_str(), |
| 157 | (const void **)&data, &length); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 158 | if (rc < 0) |
| 159 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 160 | // Metadata variable not found, check next metadata variable. |
| 161 | i++; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 162 | continue; |
| 163 | } |
| 164 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 165 | // Metadata variable found, save it and remove it from the set. |
| 166 | additionalData.emplace_back(data, length); |
| 167 | i = metalist.erase(i); |
| 168 | } |
| 169 | if (metalist.empty()) |
| 170 | { |
| 171 | // All metadata variables found, break out of journal loop. |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 172 | break; |
| 173 | } |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 174 | } |
| 175 | if (!metalist.empty()) |
| 176 | { |
| 177 | // Not all the metadata variables were found in the journal. |
| 178 | for (auto& metaVarStr : metalist) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 179 | { |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 180 | logging::log<logging::level::INFO>("Failed to find metadata", |
| 181 | logging::entry("META_FIELD=%s", metaVarStr.c_str())); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 185 | sd_journal_close(j); |
| 186 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 187 | // Create error Entry dbus object |
| 188 | entryId++; |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 189 | if (errLvl >= Entry::sevLowerLimit) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 190 | { |
| 191 | infoErrors.push_back(entryId); |
| 192 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 193 | else |
| 194 | { |
| 195 | realErrors.push_back(entryId); |
| 196 | } |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 197 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 198 | std::chrono::system_clock::now().time_since_epoch()).count(); |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 199 | auto objPath = std::string(OBJ_ENTRY) + '/' + |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 200 | std::to_string(entryId); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 201 | |
Deepak Kodihalli | 35b4637 | 2017-02-27 04:58:18 -0600 | [diff] [blame] | 202 | AssociationList objects {}; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 203 | processMetadata(errMsg, additionalData, objects); |
| 204 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 205 | auto e = std::make_unique<Entry>( |
| 206 | busLog, |
| 207 | objPath, |
| 208 | entryId, |
| 209 | ms, // Milliseconds since 1970 |
Deepak Kodihalli | 6fd9dc4 | 2018-04-03 02:08:42 -0500 | [diff] [blame] | 210 | errLvl, |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 211 | std::move(errMsg), |
| 212 | std::move(additionalData), |
| 213 | std::move(objects), |
Matt Spinler | 375ac9b | 2018-05-01 15:20:55 -0500 | [diff] [blame] | 214 | fwVersion, |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 215 | *this); |
| 216 | serialize(*e); |
| 217 | entries.insert(std::make_pair(entryId, std::move(e))); |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 220 | void Manager::processMetadata(const std::string& errorName, |
| 221 | const std::vector<std::string>& additionalData, |
| 222 | AssociationList& objects) const |
| 223 | { |
| 224 | // additionalData is a list of "metadata=value" |
| 225 | constexpr auto separator = '='; |
| 226 | for(const auto& entry: additionalData) |
| 227 | { |
| 228 | auto found = entry.find(separator); |
| 229 | if(std::string::npos != found) |
| 230 | { |
| 231 | auto metadata = entry.substr(0, found); |
| 232 | auto iter = meta.find(metadata); |
| 233 | if(meta.end() != iter) |
| 234 | { |
| 235 | (iter->second)(metadata, additionalData, objects); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 241 | void Manager::erase(uint32_t entryId) |
| 242 | { |
| 243 | auto entry = entries.find(entryId); |
| 244 | if(entries.end() != entry) |
| 245 | { |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 246 | // Delete the persistent representation of this error. |
| 247 | fs::path errorPath(ERRLOG_PERSIST_PATH); |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 248 | errorPath /= std::to_string(entryId); |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 249 | fs::remove(errorPath); |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 250 | |
| 251 | auto removeId = [](std::list<uint32_t>& ids , uint32_t id) |
| 252 | { |
| 253 | auto it = std::find(ids.begin(), ids.end(), id); |
| 254 | if (it != ids.end()) |
| 255 | { |
| 256 | ids.erase(it); |
| 257 | } |
| 258 | }; |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 259 | if (entry->second->severity() >= Entry::sevLowerLimit) |
| 260 | { |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 261 | removeId(infoErrors, entryId); |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | removeId(realErrors, entryId); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 266 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 267 | entries.erase(entry); |
| 268 | } |
Marri Devender Rao | 8959efc | 2017-11-17 00:13:41 -0600 | [diff] [blame] | 269 | else |
| 270 | { |
| 271 | logging::log<level::ERR>("Invalid entry ID to delete", |
| 272 | logging::entry("ID=%d", entryId)); |
| 273 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 274 | } |
| 275 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 276 | void Manager::restore() |
| 277 | { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 278 | auto sanity = [](const auto& id, const auto& restoredId) |
| 279 | { |
| 280 | return id == restoredId; |
| 281 | }; |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 282 | std::vector<uint32_t> errorIds; |
| 283 | |
| 284 | fs::path dir(ERRLOG_PERSIST_PATH); |
| 285 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 286 | { |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | for(auto& file: fs::directory_iterator(dir)) |
| 291 | { |
| 292 | auto id = file.path().filename().c_str(); |
| 293 | auto idNum = std::stol(id); |
| 294 | auto e = std::make_unique<Entry>( |
| 295 | busLog, |
| 296 | std::string(OBJ_ENTRY) + '/' + id, |
| 297 | idNum, |
| 298 | *this); |
| 299 | if (deserialize(file.path(), *e)) |
| 300 | { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 301 | //validate the restored error entry id |
| 302 | if (sanity(static_cast<uint32_t>(idNum), e->id())) |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 303 | { |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 304 | e->emit_object_added(); |
| 305 | if (e->severity() >= Entry::sevLowerLimit) |
| 306 | { |
| 307 | infoErrors.push_back(idNum); |
| 308 | } |
Nagaraju Goruganti | e4b0b77 | 2017-11-30 02:12:45 -0600 | [diff] [blame] | 309 | else |
| 310 | { |
| 311 | realErrors.push_back(idNum); |
| 312 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 313 | |
| 314 | entries.insert(std::make_pair(idNum, std::move(e))); |
| 315 | errorIds.push_back(idNum); |
Nagaraju Goruganti | f8a5a79 | 2017-10-13 08:09:52 -0500 | [diff] [blame] | 316 | } |
Marri Devender Rao | 979ed1c | 2017-11-17 05:06:25 -0600 | [diff] [blame] | 317 | else |
| 318 | { |
| 319 | logging::log<logging::level::ERR>( |
| 320 | "Failed in sanity check while restoring error entry. " |
| 321 | "Ignoring error entry", |
| 322 | logging::entry("ID_NUM=%d", idNum), |
| 323 | logging::entry("ENTRY_ID=%d", e->id())); |
| 324 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Vishwanatha Subbanna | 37af9ba | 2017-09-28 16:33:53 +0530 | [diff] [blame] | 328 | if (!errorIds.empty()) |
| 329 | { |
| 330 | entryId = *(std::max_element(errorIds.begin(), errorIds.end())); |
| 331 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 332 | } |
| 333 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 334 | void Manager::journalSync() |
| 335 | { |
| 336 | bool syncRequested = false; |
| 337 | auto fd = -1; |
| 338 | auto rc = -1; |
| 339 | auto wd = -1; |
| 340 | auto bus = sdbusplus::bus::new_default(); |
| 341 | |
| 342 | auto start = |
| 343 | duration_cast<microseconds>(steady_clock::now().time_since_epoch()) |
| 344 | .count(); |
| 345 | |
| 346 | constexpr auto maxRetry = 2; |
| 347 | for (int i = 0; i < maxRetry; i++) |
| 348 | { |
| 349 | // Read timestamp from synced file |
| 350 | constexpr auto syncedPath = "/run/systemd/journal/synced"; |
| 351 | std::ifstream syncedFile(syncedPath); |
| 352 | if (syncedFile.fail()) |
| 353 | { |
| 354 | log<level::ERR>("Failed to open journal synced file", |
| 355 | entry("FILENAME=%s", syncedPath), |
| 356 | entry("ERRNO=%d", errno)); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | // See if a sync happened by now |
| 361 | std::string timestampStr; |
| 362 | std::getline(syncedFile, timestampStr); |
| 363 | auto timestamp = stoll(timestampStr); |
| 364 | if (timestamp >= start) |
| 365 | { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | // Let's ask for a sync, but only once |
| 370 | if (!syncRequested) |
| 371 | { |
| 372 | syncRequested = true; |
| 373 | |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 374 | constexpr auto JOURNAL_UNIT = "systemd-journald.service"; |
| 375 | auto signal = SIGRTMIN + 1; |
| 376 | |
| 377 | auto method = bus.new_method_call(SYSTEMD_BUSNAME, SYSTEMD_PATH, |
| 378 | SYSTEMD_INTERFACE, "KillUnit"); |
| 379 | method.append(JOURNAL_UNIT, "main", signal); |
| 380 | bus.call(method); |
| 381 | if (method.is_method_error()) |
| 382 | { |
| 383 | log<level::ERR>("Failed to kill journal service"); |
| 384 | return; |
| 385 | } |
| 386 | continue; |
| 387 | } |
| 388 | |
| 389 | // Let's install the inotify watch, if we didn't do that yet. This watch |
| 390 | // monitors the syncedFile for when journald updates it with a newer |
| 391 | // timestamp. This means the journal has been flushed. |
| 392 | if (fd < 0) |
| 393 | { |
| 394 | fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); |
| 395 | if (fd < 0) |
| 396 | { |
| 397 | log<level::ERR>("Failed to create inotify watch", |
| 398 | entry("ERRNO=%d", errno)); |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | constexpr auto JOURNAL_RUN_PATH = "/run/systemd/journal"; |
| 403 | wd = inotify_add_watch(fd, JOURNAL_RUN_PATH, |
| 404 | IN_MOVED_TO | IN_DONT_FOLLOW | IN_ONLYDIR); |
| 405 | if (wd < 0) |
| 406 | { |
| 407 | log<level::ERR>("Failed to watch journal directory", |
| 408 | entry("PATH=%s", JOURNAL_RUN_PATH), |
| 409 | entry("ERRNO=%d", errno)); |
| 410 | close(fd); |
| 411 | return; |
| 412 | } |
| 413 | continue; |
| 414 | } |
| 415 | |
| 416 | // Let's wait until inotify reports an event |
| 417 | struct pollfd fds = { |
| 418 | .fd = fd, |
| 419 | .events = POLLIN, |
| 420 | }; |
| 421 | constexpr auto pollTimeout = 5; // 5 seconds |
| 422 | rc = poll(&fds, 1, pollTimeout * 1000); |
| 423 | if (rc < 0) |
| 424 | { |
| 425 | log<level::ERR>("Failed to add event", entry("ERRNO=%d", errno), |
| 426 | entry("ERR=%s", strerror(-rc))); |
| 427 | inotify_rm_watch(fd, wd); |
| 428 | close(fd); |
| 429 | return; |
| 430 | } |
| 431 | else if (rc == 0) |
| 432 | { |
| 433 | log<level::INFO>("Poll timeout, no new journal synced data", |
| 434 | entry("TIMEOUT=%d", pollTimeout)); |
| 435 | break; |
| 436 | } |
| 437 | |
| 438 | // Read from the specified file descriptor until there is no new data, |
| 439 | // throwing away everything read since the timestamp will be read at the |
| 440 | // beginning of the loop. |
| 441 | constexpr auto maxBytes = 64; |
| 442 | uint8_t buffer[maxBytes]; |
| 443 | while (read(fd, buffer, maxBytes) > 0) |
| 444 | ; |
| 445 | } |
| 446 | |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 447 | if (fd != -1) |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 448 | { |
Adriana Kobylak | 4a029f2 | 2018-05-23 12:58:19 -0500 | [diff] [blame] | 449 | if (wd != -1) |
| 450 | { |
| 451 | inotify_rm_watch(fd, wd); |
| 452 | } |
Adriana Kobylak | 5f4247f | 2018-03-15 10:27:05 -0500 | [diff] [blame] | 453 | close(fd); |
| 454 | } |
| 455 | |
| 456 | return; |
| 457 | } |
| 458 | |
Matt Spinler | 1275bd1 | 2018-05-01 15:13:53 -0500 | [diff] [blame] | 459 | std::string Manager::readFWVersion() |
| 460 | { |
| 461 | std::string version; |
| 462 | std::ifstream versionFile{BMC_VERSION_FILE}; |
| 463 | std::string line; |
| 464 | static constexpr auto VERSION_ID = "VERSION_ID="; |
| 465 | |
| 466 | while (std::getline(versionFile, line)) |
| 467 | { |
| 468 | if (line.find(VERSION_ID) != std::string::npos) |
| 469 | { |
| 470 | auto pos = line.find_first_of('"') + 1; |
| 471 | version = line.substr(pos, line.find_last_of('"') - pos); |
| 472 | break; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if (version.empty()) |
| 477 | { |
| 478 | log<level::ERR>("Unable to read BMC firmware version"); |
| 479 | } |
| 480 | |
| 481 | return version; |
| 482 | } |
| 483 | |
Nagaraju Goruganti | 05aae8b | 2017-08-30 07:56:12 -0500 | [diff] [blame] | 484 | } // namespace internal |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 485 | } // namespace logging |
| 486 | } // namepsace phosphor |