blob: e55211f8c1bd080aeaaf2fa7ff1de8f2614d57b5 [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
Patrick Williams9b18bf22022-07-22 19:26:55 -050037Watch::Watch(sdbusplus::bus_t& 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{
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050050 std::filesystem::path file(ELOG_ID_PERSIST_PATH);
51 if (std::filesystem::exists(file))
Jayanth Othayoth24964822017-09-04 22:07:06 -050052 {
53 if (!deserialize(ELOG_ID_PERSIST_PATH, elogList))
54 {
55 log<level::ERR>("Error occurred during error id deserialize");
56 }
57 }
58}
59
Patrick Williams9b18bf22022-07-22 19:26:55 -050060void Watch::addCallback(sdbusplus::message_t& msg)
Jayanth Othayothd0f00642017-09-04 06:26:30 -050061{
Jayanth Othayothd0f00642017-09-04 06:26:30 -050062 using QuotaExceeded =
63 sdbusplus::xyz::openbmc_project::Dump::Create::Error::QuotaExceeded;
64
William A. Kennington III90d147a2018-06-12 16:42:33 -070065 sdbusplus::message::object_path objectPath;
66 PropertyMap propertyMap;
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070067 try
68 {
William A. Kennington III90d147a2018-06-12 16:42:33 -070069 msg.read(objectPath, propertyMap);
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070070 }
Patrick Williams9b18bf22022-07-22 19:26:55 -050071 catch (const sdbusplus::exception_t& e)
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070072 {
George Liu858fbb22021-07-01 12:25:44 +080073 log<level::ERR>(
74 fmt::format(
75 "Failed to parse elog add signal, errormsg({}), REPLY_SIG({})",
76 e.what(), msg.get_signature())
77 .c_str());
William A. Kennington III15cd3ce2018-05-15 11:34:44 -070078 return;
79 }
Jayanth Othayothd0f00642017-09-04 06:26:30 -050080
William A. Kennington III90d147a2018-06-12 16:42:33 -070081 std::size_t found = objectPath.str.find("entry");
Jayanth Othayothd0f00642017-09-04 06:26:30 -050082 if (found == std::string::npos)
83 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050084 // Not a new error entry skip
Jayanth Othayothd0f00642017-09-04 06:26:30 -050085 return;
86 }
87
Jayanth Othayoth24964822017-09-04 22:07:06 -050088 auto eId = getEid(objectPath);
89
90 auto search = elogList.find(eId);
91 if (search != elogList.end())
92 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050093 // elog exists in the list, Skip the dump
Jayanth Othayoth24964822017-09-04 22:07:06 -050094 return;
95 }
96
William A. Kennington III90d147a2018-06-12 16:42:33 -070097 auto iter = propertyMap.find("xyz.openbmc_project.Logging.Entry");
98 if (iter == propertyMap.end())
Jayanth Othayothd0f00642017-09-04 06:26:30 -050099 {
100 return;
101 }
102
103 auto attr = iter->second.find("Message");
104 if (attr == iter->second.end())
105 {
106 return;
107 }
108
Patrick Williams07f0f462020-05-13 11:58:11 -0500109 auto& data = std::get<PropertyName>(attr->second);
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500110 if (data.empty())
111 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500112 // No Message skip
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500113 return;
114 }
115
Marri Devender Rao0deb2872018-11-12 07:45:54 -0600116 EType errorType;
117 for (const auto& [type, errorList] : errorMap)
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500118 {
Marri Devender Rao0deb2872018-11-12 07:45:54 -0600119 auto error = std::find(errorList.begin(), errorList.end(), data);
120 if (error != errorList.end())
121 {
122 errorType = type;
123 break;
124 }
125 }
126
127 // error not supported in the configuration
128 if (errorType.empty())
129 {
Jayanth Othayothd0f00642017-09-04 06:26:30 -0500130 return;
131 }
132
133 std::vector<std::string> fullPaths;
134 fullPaths.push_back(objectPath);
135
136 try
137 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -0500138 // Save the elog information. This is to avoid dump requests
139 // in elog restore path.
Jayanth Othayoth24964822017-09-04 22:07:06 -0500140 elogList.insert(eId);
141
142 phosphor::dump::elog::serialize(elogList);
143
Patrick Williams78e88402023-05-10 07:50:48 -0500144 auto item = std::find_if(phosphor::dump::bmc::TypeMap.begin(),
145 phosphor::dump::bmc::TypeMap.end(),
146 [errorType](const auto& err) {
147 return (err.second == errorType);
148 });
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
Patrick Williams9b18bf22022-07-22 19:26:55 -0500161void Watch::delCallback(sdbusplus::message_t& msg)
Jayanth Othayoth24964822017-09-04 22:07:06 -0500162{
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 Williams9b18bf22022-07-22 19:26:55 -0500168 catch (const sdbusplus::exception_t& 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