Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 1 | #include <fstream> |
| 2 | #include <iostream> |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 3 | #include <chrono> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 4 | #include <cstdio> |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 5 | #include <set> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 6 | #include <string> |
| 7 | #include <vector> |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 8 | #include <sdbusplus/vtable.hpp> |
| 9 | #include <systemd/sd-bus.h> |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 10 | #include <systemd/sd-journal.h> |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 11 | #include "config.h" |
| 12 | #include "elog_entry.hpp" |
Saqib Khan | 2bb1519 | 2017-02-13 13:19:55 -0600 | [diff] [blame] | 13 | #include <phosphor-logging/log.hpp> |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 14 | #include "log_manager.hpp" |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 15 | #include "elog_meta.hpp" |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 16 | #include "elog_serialize.hpp" |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 17 | |
| 18 | using namespace phosphor::logging; |
| 19 | extern const std::map<metadata::Metadata, |
| 20 | std::function<metadata::associations::Type>> meta; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 21 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 22 | namespace phosphor |
| 23 | { |
| 24 | namespace logging |
| 25 | { |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 26 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 27 | void Manager::commit(uint64_t transactionId, std::string errMsg) |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 28 | { |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 29 | if (capped) |
| 30 | { |
| 31 | return; |
| 32 | } |
| 33 | if (entries.size() >= ERROR_CAP) |
| 34 | { |
| 35 | log<level::ERR>("Reached error cap, Ignoring error", |
| 36 | entry("SIZE=%d", entries.size()), |
| 37 | entry("ERROR_CAP=%d", ERROR_CAP)); |
| 38 | capped = true; |
| 39 | return; |
| 40 | } |
| 41 | |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 42 | constexpr const auto transactionIdVar = "TRANSACTION_ID"; |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 43 | // Length of 'TRANSACTION_ID' string. |
Adriana Kobylak | 6721899 | 2017-02-28 12:53:37 -0600 | [diff] [blame] | 44 | constexpr const auto transactionIdVarSize = strlen(transactionIdVar); |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 45 | // Length of 'TRANSACTION_ID=' string. |
| 46 | constexpr const auto transactionIdVarOffset = transactionIdVarSize + 1; |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 47 | |
Adriana Kobylak | cfd9a7d | 2017-06-07 11:57:31 -0500 | [diff] [blame] | 48 | // Flush all the pending log messages into the journal via Synchronize |
| 49 | constexpr auto JOURNAL_BUSNAME = "org.freedesktop.journal1"; |
| 50 | constexpr auto JOURNAL_PATH = "/org/freedesktop/journal1"; |
| 51 | constexpr auto JOURNAL_INTERFACE = "org.freedesktop.journal1"; |
| 52 | auto bus = sdbusplus::bus::new_default(); |
| 53 | auto method = bus.new_method_call(JOURNAL_BUSNAME, JOURNAL_PATH, |
| 54 | JOURNAL_INTERFACE, "Synchronize"); |
| 55 | bus.call_noreply(method); |
| 56 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 57 | sd_journal *j = nullptr; |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 58 | int rc = sd_journal_open(&j, SD_JOURNAL_LOCAL_ONLY); |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 59 | if (rc < 0) |
| 60 | { |
| 61 | logging::log<logging::level::ERR>("Failed to open journal", |
| 62 | logging::entry("DESCRIPTION=%s", strerror(-rc))); |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 63 | return; |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 64 | } |
| 65 | |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 66 | std::string transactionIdStr = std::to_string(transactionId); |
Adriana Kobylak | d722b3a | 2017-02-28 12:10:44 -0600 | [diff] [blame] | 67 | std::set<std::string> metalist; |
| 68 | auto metamap = g_errMetaMap.find(errMsg); |
| 69 | if (metamap != g_errMetaMap.end()) |
| 70 | { |
| 71 | metalist.insert(metamap->second.begin(), metamap->second.end()); |
| 72 | } |
Adriana Kobylak | 7298dc2 | 2017-01-24 12:21:50 -0600 | [diff] [blame] | 73 | |
Jayanth Othayoth | db18ebe | 2017-09-04 00:48:02 -0500 | [diff] [blame^] | 74 | //Add _PID field information in AdditionalData. |
| 75 | metalist.insert("_PID"); |
| 76 | |
Tom Joseph | 7a33ee4 | 2017-07-25 00:04:20 +0530 | [diff] [blame] | 77 | std::vector<std::string> additionalData; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 78 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 79 | // Read the journal from the end to get the most recent entry first. |
| 80 | // The result from the sd_journal_get_data() is of the form VARIABLE=value. |
| 81 | SD_JOURNAL_FOREACH_BACKWARDS(j) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 82 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 83 | const char *data = nullptr; |
| 84 | size_t length = 0; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 85 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 86 | // Look for the transaction id metadata variable |
| 87 | rc = sd_journal_get_data(j, transactionIdVar, (const void **)&data, |
| 88 | &length); |
| 89 | if (rc < 0) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 90 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 91 | // This journal entry does not have the TRANSACTION_ID |
| 92 | // metadata variable. |
| 93 | continue; |
| 94 | } |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 95 | |
Adriana Kobylak | 27c87d9 | 2017-03-06 12:45:09 -0600 | [diff] [blame] | 96 | // journald does not guarantee that sd_journal_get_data() returns NULL |
| 97 | // terminated strings, so need to specify the size to use to compare, |
| 98 | // use the returned length instead of anything that relies on NULL |
| 99 | // terminators like strlen(). |
| 100 | // The data variable is in the form of 'TRANSACTION_ID=1234'. Remove |
| 101 | // the TRANSACTION_ID characters plus the (=) sign to do the comparison. |
| 102 | // 'data + transactionIdVarOffset' will be in the form of '1234'. |
| 103 | // 'length - transactionIdVarOffset' will be the length of '1234'. |
| 104 | if ((length <= (transactionIdVarOffset)) || |
| 105 | (transactionIdStr.compare(0, |
| 106 | transactionIdStr.size(), |
| 107 | data + transactionIdVarOffset, |
| 108 | length - transactionIdVarOffset) != 0)) |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 109 | { |
| 110 | // The value of the TRANSACTION_ID metadata is not the requested |
| 111 | // transaction id number. |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | // Search for all metadata variables in the current journal entry. |
| 116 | for (auto i = metalist.cbegin(); i != metalist.cend();) |
| 117 | { |
| 118 | rc = sd_journal_get_data(j, (*i).c_str(), |
| 119 | (const void **)&data, &length); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 120 | if (rc < 0) |
| 121 | { |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 122 | // Metadata variable not found, check next metadata variable. |
| 123 | i++; |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 124 | continue; |
| 125 | } |
| 126 | |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 127 | // Metadata variable found, save it and remove it from the set. |
| 128 | additionalData.emplace_back(data, length); |
| 129 | i = metalist.erase(i); |
| 130 | } |
| 131 | if (metalist.empty()) |
| 132 | { |
| 133 | // All metadata variables found, break out of journal loop. |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 134 | break; |
| 135 | } |
Adriana Kobylak | fbe8872 | 2017-02-22 16:49:59 -0600 | [diff] [blame] | 136 | } |
| 137 | if (!metalist.empty()) |
| 138 | { |
| 139 | // Not all the metadata variables were found in the journal. |
| 140 | for (auto& metaVarStr : metalist) |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 141 | { |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 142 | logging::log<logging::level::INFO>("Failed to find metadata", |
| 143 | logging::entry("META_FIELD=%s", metaVarStr.c_str())); |
Adriana Kobylak | 9aa7d78 | 2017-02-18 09:20:49 -0600 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Adriana Kobylak | d311bc8 | 2016-10-16 09:54:40 -0500 | [diff] [blame] | 147 | sd_journal_close(j); |
| 148 | |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 149 | // Create error Entry dbus object |
| 150 | entryId++; |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 151 | auto ms = std::chrono::duration_cast<std::chrono::milliseconds>( |
| 152 | std::chrono::system_clock::now().time_since_epoch()).count(); |
Adriana Kobylak | 4ea7f31 | 2017-01-10 12:52:34 -0600 | [diff] [blame] | 153 | auto objPath = std::string(OBJ_ENTRY) + '/' + |
Adriana Kobylak | c5f0bbd | 2017-01-22 14:56:04 -0600 | [diff] [blame] | 154 | std::to_string(entryId); |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 155 | |
Deepak Kodihalli | 35b4637 | 2017-02-27 04:58:18 -0600 | [diff] [blame] | 156 | AssociationList objects {}; |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 157 | processMetadata(errMsg, additionalData, objects); |
| 158 | |
Marri Devender Rao | 0a71bad | 2017-07-12 08:01:39 -0500 | [diff] [blame] | 159 | level reqLevel = level::ERR; // Default to ERR |
Adriana Kobylak | d722b3a | 2017-02-28 12:10:44 -0600 | [diff] [blame] | 160 | auto levelmap = g_errLevelMap.find(errMsg); |
| 161 | if (levelmap != g_errLevelMap.end()) |
| 162 | { |
| 163 | reqLevel = levelmap->second; |
| 164 | } |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 165 | auto e = std::make_unique<Entry>( |
| 166 | busLog, |
| 167 | objPath, |
| 168 | entryId, |
| 169 | ms, // Milliseconds since 1970 |
| 170 | static_cast<Entry::Level>(reqLevel), |
| 171 | std::move(errMsg), |
| 172 | std::move(additionalData), |
| 173 | std::move(objects), |
| 174 | *this); |
| 175 | serialize(*e); |
| 176 | entries.insert(std::make_pair(entryId, std::move(e))); |
Adriana Kobylak | 1db1bd3 | 2016-10-10 11:39:20 -0500 | [diff] [blame] | 177 | } |
| 178 | |
Deepak Kodihalli | a87c157 | 2017-02-28 07:40:34 -0600 | [diff] [blame] | 179 | void Manager::processMetadata(const std::string& errorName, |
| 180 | const std::vector<std::string>& additionalData, |
| 181 | AssociationList& objects) const |
| 182 | { |
| 183 | // additionalData is a list of "metadata=value" |
| 184 | constexpr auto separator = '='; |
| 185 | for(const auto& entry: additionalData) |
| 186 | { |
| 187 | auto found = entry.find(separator); |
| 188 | if(std::string::npos != found) |
| 189 | { |
| 190 | auto metadata = entry.substr(0, found); |
| 191 | auto iter = meta.find(metadata); |
| 192 | if(meta.end() != iter) |
| 193 | { |
| 194 | (iter->second)(metadata, additionalData, objects); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 200 | void Manager::erase(uint32_t entryId) |
| 201 | { |
| 202 | auto entry = entries.find(entryId); |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 203 | auto id = entry->second->id(); |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 204 | if(entries.end() != entry) |
| 205 | { |
Deepak Kodihalli | 3388799 | 2017-06-13 07:06:49 -0500 | [diff] [blame] | 206 | // Delete the persistent representation of this error. |
| 207 | fs::path errorPath(ERRLOG_PERSIST_PATH); |
| 208 | errorPath /= std::to_string(id); |
| 209 | fs::remove(errorPath); |
| 210 | |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 211 | entries.erase(entry); |
| 212 | } |
Marri Devender Rao | 7656fba | 2017-08-06 05:42:52 -0500 | [diff] [blame] | 213 | |
| 214 | if (entries.size() < ERROR_CAP) |
| 215 | { |
| 216 | capped = false; |
| 217 | } |
Deepak Kodihalli | 99a8549 | 2017-03-31 06:01:57 -0500 | [diff] [blame] | 218 | } |
| 219 | |
Deepak Kodihalli | 72654f1 | 2017-06-12 04:33:29 -0500 | [diff] [blame] | 220 | void Manager::restore() |
| 221 | { |
| 222 | std::vector<uint32_t> errorIds; |
| 223 | |
| 224 | fs::path dir(ERRLOG_PERSIST_PATH); |
| 225 | if (!fs::exists(dir) || fs::is_empty(dir)) |
| 226 | { |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | for(auto& file: fs::directory_iterator(dir)) |
| 231 | { |
| 232 | auto id = file.path().filename().c_str(); |
| 233 | auto idNum = std::stol(id); |
| 234 | auto e = std::make_unique<Entry>( |
| 235 | busLog, |
| 236 | std::string(OBJ_ENTRY) + '/' + id, |
| 237 | idNum, |
| 238 | *this); |
| 239 | if (deserialize(file.path(), *e)) |
| 240 | { |
| 241 | e->emit_object_added(); |
| 242 | entries.insert(std::make_pair(idNum, std::move(e))); |
| 243 | errorIds.push_back(idNum); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | entryId = *(std::max_element(errorIds.begin(), errorIds.end())); |
| 248 | } |
| 249 | |
Adriana Kobylak | 8f7941e | 2016-11-14 14:46:23 -0600 | [diff] [blame] | 250 | } // namespace logging |
| 251 | } // namepsace phosphor |