blob: e95c9918a679cf54ba1ff8a6c282960ba8c0e287 [file] [log] [blame]
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05001#include "config.h"
2
3#include "elog_watch.hpp"
4
5#include "dump_internal.hpp"
6#include "dump_serialize.hpp"
Marri Devender Rao0deb2872018-11-12 07:45:54 -06007#include "errors_map.hpp"
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05008#include "xyz/openbmc_project/Dump/Create/error.hpp"
9
George Liu858fbb22021-07-01 12:25:44 +080010#include <fmt/core.h>
11
Vishwanatha Subbanna31085972017-10-05 17:06:37 +053012#include <cereal/cereal.hpp>
Jayanth Othayothd0f00642017-09-04 06:26:30 -050013#include <phosphor-logging/elog.hpp>
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070014#include <sdbusplus/exception.hpp>
Jayanth Othayothd0f00642017-09-04 06:26:30 -050015
Jayanth Othayoth0af74a52021-04-08 03:55:21 -050016#include <fstream>
17
Vishwanatha Subbanna31085972017-10-05 17:06:37 +053018// Register class version with Cereal
Ramesh Iyyarbb410df2020-08-03 03:13:04 -050019CEREAL_CLASS_VERSION(phosphor::dump::elog::Watch, CLASS_VERSION)
Jayanth Othayothd0f00642017-09-04 06:26:30 -050020
21namespace phosphor
22{
23namespace dump
24{
25namespace elog
26{
27
28using namespace phosphor::logging;
29constexpr auto LOG_PATH = "/xyz/openbmc_project/logging";
Jayanth Othayothd0f00642017-09-04 06:26:30 -050030using Message = std::string;
Patrick Williams984a98f2020-05-13 17:53:32 -050031using Attributes = std::variant<Message>;
Jayanth Othayothd0f00642017-09-04 06:26:30 -050032using AttributeName = std::string;
33using AttributeMap = std::map<AttributeName, Attributes>;
34using PropertyName = std::string;
35using PropertyMap = std::map<PropertyName, AttributeMap>;
Jayanth Othayothd0f00642017-09-04 06:26:30 -050036
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050037Watch::Watch(sdbusplus::bus::bus& bus, IMgr& iMgr) :
Jayanth Othayoth24964822017-09-04 22:07:06 -050038 iMgr(iMgr),
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050039 addMatch(bus,
40 sdbusplus::bus::match::rules::interfacesAdded() +
41 sdbusplus::bus::match::rules::path_namespace(OBJ_LOGGING),
42 std::bind(std::mem_fn(&Watch::addCallback), this,
43 std::placeholders::_1)),
44 delMatch(bus,
45 sdbusplus::bus::match::rules::interfacesRemoved() +
46 sdbusplus::bus::match::rules::path_namespace(OBJ_LOGGING),
47 std::bind(std::mem_fn(&Watch::delCallback), this,
48 std::placeholders::_1))
Jayanth Othayoth24964822017-09-04 22:07:06 -050049{
50
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050051 std::filesystem::path file(ELOG_ID_PERSIST_PATH);
52 if (std::filesystem::exists(file))
Jayanth Othayoth24964822017-09-04 22:07:06 -050053 {
54 if (!deserialize(ELOG_ID_PERSIST_PATH, elogList))
55 {
56 log<level::ERR>("Error occurred during error id deserialize");
57 }
58 }
59}
60
61void Watch::addCallback(sdbusplus::message::message& msg)
Jayanth Othayothd0f00642017-09-04 06:26:30 -050062{
Jayanth Othayothd0f00642017-09-04 06:26:30 -050063 using QuotaExceeded =
64 sdbusplus::xyz::openbmc_project::Dump::Create::Error::QuotaExceeded;
65
William A. Kennington III90d147a2018-06-12 16:42:33 -070066 sdbusplus::message::object_path objectPath;
67 PropertyMap propertyMap;
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070068 try
69 {
William A. Kennington III90d147a2018-06-12 16:42:33 -070070 msg.read(objectPath, propertyMap);
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070071 }
Patrick Williams52ac69c2021-09-02 09:37:16 -050072 catch (const sdbusplus::exception::exception& e)
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070073 {
George Liu858fbb22021-07-01 12:25:44 +080074 log<level::ERR>(
75 fmt::format(
76 "Failed to parse elog add signal, errormsg({}), REPLY_SIG({})",
77 e.what(), msg.get_signature())
78 .c_str());
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070079 return;
80 }
Jayanth Othayothd0f00642017-09-04 06:26:30 -050081
William A. Kennington III90d147a2018-06-12 16:42:33 -070082 std::size_t found = objectPath.str.find("entry");
Jayanth Othayothd0f00642017-09-04 06:26:30 -050083 if (found == std::string::npos)
84 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050085 // Not a new error entry skip
Jayanth Othayothd0f00642017-09-04 06:26:30 -050086 return;
87 }
88
Jayanth Othayoth24964822017-09-04 22:07:06 -050089 auto eId = getEid(objectPath);
90
91 auto search = elogList.find(eId);
92 if (search != elogList.end())
93 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050094 // elog exists in the list, Skip the dump
Jayanth Othayoth24964822017-09-04 22:07:06 -050095 return;
96 }
97
William A. Kennington III90d147a2018-06-12 16:42:33 -070098 auto iter = propertyMap.find("xyz.openbmc_project.Logging.Entry");
99 if (iter == propertyMap.end())
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500100 {
101 return;
102 }
103
104 auto attr = iter->second.find("Message");
105 if (attr == iter->second.end())
106 {
107 return;
108 }
109
Patrick Williams07f0f462020-05-13 11:58:11 -0500110 auto& data = std::get<PropertyName>(attr->second);
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500111 if (data.empty())
112 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500113 // No Message skip
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500114 return;
115 }
116
Marri Devender Rao0deb2872018-11-12 07:45:54 -0600117 EType errorType;
118 for (const auto& [type, errorList] : errorMap)
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500119 {
Marri Devender Rao0deb2872018-11-12 07:45:54 -0600120 auto error = std::find(errorList.begin(), errorList.end(), data);
121 if (error != errorList.end())
122 {
123 errorType = type;
124 break;
125 }
126 }
127
128 // error not supported in the configuration
129 if (errorType.empty())
130 {
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500131 return;
132 }
133
134 std::vector<std::string> fullPaths;
135 fullPaths.push_back(objectPath);
136
137 try
138 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500139 // Save the elog information. This is to avoid dump requests
140 // in elog restore path.
Jayanth Othayoth24964822017-09-04 22:07:06 -0500141 elogList.insert(eId);
142
143 phosphor::dump::elog::serialize(elogList);
144
Marri Devender Rao0deb2872018-11-12 07:45:54 -0600145 auto item = std::find_if(
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500146 phosphor::dump::bmc::TypeMap.begin(),
147 phosphor::dump::bmc::TypeMap.end(),
Marri Devender Rao0deb2872018-11-12 07:45:54 -0600148 [errorType](const auto& err) { return (err.second == errorType); });
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500149 if (item != phosphor::dump::bmc::TypeMap.end())
Marri Devender Rao0deb2872018-11-12 07:45:54 -0600150 {
151 iMgr.IMgr::create((*item).first, fullPaths);
152 }
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500153 }
Patrick Williams9d2d7222021-10-06 12:44:44 -0500154 catch (const QuotaExceeded& e)
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500155 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500156 // No action now
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500157 }
158 return;
159}
160
Jayanth Othayoth24964822017-09-04 22:07:06 -0500161void Watch::delCallback(sdbusplus::message::message& msg)
162{
William A. Kennington III90d147a2018-06-12 16:42:33 -0700163 sdbusplus::message::object_path objectPath;
William A. Kennington III15cd3ce2018-05-15 11:34:44 -0700164 try
165 {
William A. Kennington III90d147a2018-06-12 16:42:33 -0700166 msg.read(objectPath);
William A. Kennington III15cd3ce2018-05-15 11:34:44 -0700167 }
Patrick Williams52ac69c2021-09-02 09:37:16 -0500168 catch (const sdbusplus::exception::exception& e)
William A. Kennington III15cd3ce2018-05-15 11:34:44 -0700169 {
George Liu858fbb22021-07-01 12:25:44 +0800170 log<level::ERR>(
171 fmt::format(
172 "Failed to parse elog del signal, errormsg({}), REPLY_SIG({})",
173 e.what(), msg.get_signature())
174 .c_str());
William A. Kennington III15cd3ce2018-05-15 11:34:44 -0700175 return;
176 }
Jayanth Othayoth24964822017-09-04 22:07:06 -0500177
Andrew Geissler638b43f2020-04-16 13:33:33 -0500178 std::size_t found = objectPath.str.find("entry");
179 if (found == std::string::npos)
180 {
181 // Not a error entry so skip
182 return;
183 }
184
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500185 // Get elog id
Jayanth Othayoth24964822017-09-04 22:07:06 -0500186 auto eId = getEid(objectPath);
187
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500188 // Delete the elog entry from the list and serialize
Jayanth Othayoth24964822017-09-04 22:07:06 -0500189 auto search = elogList.find(eId);
190 if (search != elogList.end())
191 {
192 elogList.erase(search);
193 phosphor::dump::elog::serialize(elogList);
194 }
195}
196
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500197} // namespace elog
198} // namespace dump
199} // namespace phosphor