blob: 5e053422778c1649e7bbfcea5d1a9304639115ce [file] [log] [blame]
Matt Spinlere0017eb2018-03-27 11:17:38 -05001/**
2 * Copyright © 2018 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#include "config.h"
Matt Spinler66e07072018-09-12 10:36:14 -050017
Matt Spinlere0017eb2018-03-27 11:17:38 -050018#include "manager.hpp"
Matt Spinler66e07072018-09-12 10:36:14 -050019
20#include "callout.hpp"
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050021#include "policy_find.hpp"
Matt Spinlere0017eb2018-03-27 11:17:38 -050022
Matt Spinler66e07072018-09-12 10:36:14 -050023#include <phosphor-logging/log.hpp>
24
Matt Spinlere0017eb2018-03-27 11:17:38 -050025namespace ibm
26{
27namespace logging
28{
29
Matt Spinler52ee71b2018-05-23 13:18:55 -050030namespace fs = std::experimental::filesystem;
Matt Spinler34af47f2018-07-16 14:07:03 -050031using namespace phosphor::logging;
32using sdbusplus::exception::SdBusError;
Matt Spinler52ee71b2018-05-23 13:18:55 -050033
Matt Spinlere0017eb2018-03-27 11:17:38 -050034Manager::Manager(sdbusplus::bus::bus& bus) :
Matt Spinler259e7272018-03-29 10:57:17 -050035 bus(bus),
36 addMatch(bus,
37 sdbusplus::bus::match::rules::interfacesAdded() +
38 sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
39 std::bind(std::mem_fn(&Manager::interfaceAdded), this,
Matt Spinler055da3b2018-05-09 15:51:20 -050040 std::placeholders::_1)),
41 removeMatch(bus,
42 sdbusplus::bus::match::rules::interfacesRemoved() +
43 sdbusplus::bus::match::rules::path_namespace(LOGGING_PATH),
44 std::bind(std::mem_fn(&Manager::interfaceRemoved), this,
45 std::placeholders::_1))
Matt Spinler743e5822018-03-27 13:44:50 -050046#ifdef USE_POLICY_INTERFACE
Matt Spinler259e7272018-03-29 10:57:17 -050047 ,
48 policies(POLICY_JSON_PATH)
Matt Spinler743e5822018-03-27 13:44:50 -050049#endif
Matt Spinlere0017eb2018-03-27 11:17:38 -050050{
Matt Spinler54bfa7e2018-03-27 11:28:59 -050051 createAll();
52}
53
54void Manager::createAll()
55{
Matt Spinler34af47f2018-07-16 14:07:03 -050056 try
Matt Spinler54bfa7e2018-03-27 11:28:59 -050057 {
Matt Spinler34af47f2018-07-16 14:07:03 -050058 auto objects = getManagedObjects(bus, LOGGING_BUSNAME, LOGGING_PATH);
Matt Spinler54bfa7e2018-03-27 11:28:59 -050059
Matt Spinler34af47f2018-07-16 14:07:03 -050060 for (const auto& object : objects)
Matt Spinler54bfa7e2018-03-27 11:28:59 -050061 {
Matt Spinler34af47f2018-07-16 14:07:03 -050062 const auto& interfaces = object.second;
63
64 auto propertyMap = interfaces.find(LOGGING_IFACE);
65
66 if (propertyMap != interfaces.end())
67 {
68 createWithRestore(object.first, interfaces);
69 }
Matt Spinler54bfa7e2018-03-27 11:28:59 -050070 }
71 }
Matt Spinler34af47f2018-07-16 14:07:03 -050072 catch (const SdBusError& e)
73 {
74 log<level::ERR>("sdbusplus error getting logging managed objects",
75 entry("ERROR=%s", e.what()));
76 }
Matt Spinler54bfa7e2018-03-27 11:28:59 -050077}
78
Matt Spinlere6a51592018-05-23 12:47:10 -050079void Manager::createWithRestore(const std::string& objectPath,
80 const DbusInterfaceMap& interfaces)
Matt Spinler54bfa7e2018-03-27 11:28:59 -050081{
Matt Spinlere6a51592018-05-23 12:47:10 -050082 createObject(objectPath, interfaces);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050083
Matt Spinler60f53d82018-05-23 13:23:09 -050084 restoreCalloutObjects(objectPath, interfaces);
Matt Spinlere6a51592018-05-23 12:47:10 -050085}
86
87void Manager::create(const std::string& objectPath,
88 const DbusInterfaceMap& interfaces)
89{
90 createObject(objectPath, interfaces);
91
Matt Spinler52ee71b2018-05-23 13:18:55 -050092 createCalloutObjects(objectPath, interfaces);
Matt Spinlere6a51592018-05-23 12:47:10 -050093}
94
95void Manager::createObject(const std::string& objectPath,
96 const DbusInterfaceMap& interfaces)
97{
Matt Spinler4a6ea6a2018-03-27 14:25:12 -050098#ifdef USE_POLICY_INTERFACE
Matt Spinlere6a51592018-05-23 12:47:10 -050099 auto logInterface = interfaces.find(LOGGING_IFACE);
100 createPolicyInterface(objectPath, logInterface->second);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500101#endif
Matt Spinlere0017eb2018-03-27 11:17:38 -0500102}
103
Matt Spinlera1390352018-05-23 10:48:19 -0500104void Manager::erase(EntryID id)
105{
Matt Spinler52ee71b2018-05-23 13:18:55 -0500106 fs::remove_all(getSaveDir(id));
Matt Spinler677143b2018-05-23 10:53:27 -0500107 childEntries.erase(id);
Matt Spinlera1390352018-05-23 10:48:19 -0500108 entries.erase(id);
109}
110
Matt Spinler491fc6f2018-04-23 11:00:52 -0500111void Manager::addInterface(const std::string& objectPath, InterfaceType type,
Matt Spinler54ea3ad2018-10-23 10:40:09 -0500112 std::any& object)
Matt Spinler491fc6f2018-04-23 11:00:52 -0500113{
114 auto id = getEntryID(objectPath);
115 auto entry = entries.find(id);
116
117 if (entry == entries.end())
118 {
119 InterfaceMap interfaces;
120 interfaces.emplace(type, object);
121 entries.emplace(id, std::move(interfaces));
122 }
123 else
124 {
125 entry->second.emplace(type, object);
126 }
127}
128
Matt Spinler677143b2018-05-23 10:53:27 -0500129void Manager::addChildInterface(const std::string& objectPath,
Matt Spinler54ea3ad2018-10-23 10:40:09 -0500130 InterfaceType type, std::any& object)
Matt Spinler677143b2018-05-23 10:53:27 -0500131{
132 auto id = getEntryID(objectPath);
133 auto entry = childEntries.find(id);
134
135 // childEntries is:
136 // A map of error log entry IDs to:
137 // a map of interface types to:
138 // a vector of interface objects
139
140 if (entry == childEntries.end())
141 {
142 ObjectList objects{object};
143 InterfaceMapMulti interfaces;
144 interfaces.emplace(type, std::move(objects));
145 childEntries.emplace(id, std::move(interfaces));
146 }
147 else
148 {
149 auto i = entry->second.find(type);
150 if (i == entry->second.end())
151 {
152 ObjectList objects{objects};
153 entry->second.emplace(type, objects);
154 }
155 else
156 {
157 i->second.emplace_back(object);
158 }
159 }
160}
161
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500162#ifdef USE_POLICY_INTERFACE
Matt Spinler259e7272018-03-29 10:57:17 -0500163void Manager::createPolicyInterface(const std::string& objectPath,
164 const DbusPropertyMap& properties)
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500165{
166 auto values = policy::find(policies, properties);
167
Matt Spinler259e7272018-03-29 10:57:17 -0500168 auto object = std::make_shared<PolicyObject>(bus, objectPath.c_str(), true);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500169
170 object->eventID(std::get<policy::EIDField>(values));
171 object->description(std::get<policy::MsgField>(values));
172
Matt Spinler9bea4ea2018-05-09 15:53:04 -0500173 object->emit_object_added();
174
Matt Spinler54ea3ad2018-10-23 10:40:09 -0500175 std::any anyObject = object;
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500176
Matt Spinler491fc6f2018-04-23 11:00:52 -0500177 addInterface(objectPath, InterfaceType::POLICY, anyObject);
Matt Spinler4a6ea6a2018-03-27 14:25:12 -0500178}
179#endif
180
Matt Spinler52ee71b2018-05-23 13:18:55 -0500181void Manager::createCalloutObjects(const std::string& objectPath,
182 const DbusInterfaceMap& interfaces)
183{
184 // Use the associations property in the org.openbmc.Associations
185 // interface to find any callouts. Then grab all properties on
186 // the Asset interface for that object in the inventory to use
187 // in our callout objects.
188
189 auto associations = interfaces.find(ASSOC_IFACE);
190 if (associations == interfaces.end())
191 {
192 return;
193 }
194
195 const auto& properties = associations->second;
196 auto assocProperty = properties.find("associations");
197 auto assocValue = assocProperty->second.get<AssociationsPropertyType>();
198
199 auto id = getEntryID(objectPath);
200 auto calloutNum = 0;
201 DbusSubtree subtree;
202
203 for (const auto& association : assocValue)
204 {
Matt Spinler34af47f2018-07-16 14:07:03 -0500205 try
Matt Spinler52ee71b2018-05-23 13:18:55 -0500206 {
Matt Spinler34af47f2018-07-16 14:07:03 -0500207 if (std::get<forwardPos>(association) != "callout")
208 {
209 continue;
210 }
Matt Spinler52ee71b2018-05-23 13:18:55 -0500211
Matt Spinler34af47f2018-07-16 14:07:03 -0500212 auto callout = std::get<endpointPos>(association);
Matt Spinler52ee71b2018-05-23 13:18:55 -0500213
Matt Spinler52ee71b2018-05-23 13:18:55 -0500214 if (subtree.empty())
215 {
Matt Spinler34af47f2018-07-16 14:07:03 -0500216 subtree = getSubtree(bus, "/", 0, ASSET_IFACE);
217 if (subtree.empty())
218 {
219 break;
220 }
Matt Spinler52ee71b2018-05-23 13:18:55 -0500221 }
Matt Spinler52ee71b2018-05-23 13:18:55 -0500222
Matt Spinler34af47f2018-07-16 14:07:03 -0500223 auto service = getService(callout, ASSET_IFACE, subtree);
224 if (service.empty())
225 {
226 continue;
227 }
228
229 auto properties =
230 getAllProperties(bus, service, callout, ASSET_IFACE);
231 if (properties.empty())
232 {
233 continue;
234 }
235
236 auto calloutPath = getCalloutObjectPath(objectPath, calloutNum);
237
238 auto object = std::make_shared<Callout>(
239 bus, calloutPath, callout, calloutNum,
240 getLogTimestamp(interfaces), properties);
241
242 auto dir = getCalloutSaveDir(id);
243 if (!fs::exists(dir))
244 {
245 fs::create_directories(dir);
246 }
247 object->serialize(dir);
248
Matt Spinler54ea3ad2018-10-23 10:40:09 -0500249 std::any anyObject = object;
Matt Spinler34af47f2018-07-16 14:07:03 -0500250 addChildInterface(objectPath, InterfaceType::CALLOUT, anyObject);
251 calloutNum++;
252 }
253 catch (const SdBusError& e)
Matt Spinler52ee71b2018-05-23 13:18:55 -0500254 {
Matt Spinler34af47f2018-07-16 14:07:03 -0500255 log<level::ERR>("sdbusplus exception", entry("ERROR=%s", e.what()));
Matt Spinler52ee71b2018-05-23 13:18:55 -0500256 }
Matt Spinler52ee71b2018-05-23 13:18:55 -0500257 }
258}
259
Matt Spinler60f53d82018-05-23 13:23:09 -0500260void Manager::restoreCalloutObjects(const std::string& objectPath,
261 const DbusInterfaceMap& interfaces)
262{
263 auto saveDir = getCalloutSaveDir(getEntryID(objectPath));
264
265 if (!fs::exists(saveDir))
266 {
267 return;
268 }
269
270 size_t id;
271 for (auto& f : fs::directory_iterator(saveDir))
272 {
273 try
274 {
275 id = std::stoul(f.path().filename());
276 }
277 catch (std::exception& e)
278 {
Matt Spinler60f53d82018-05-23 13:23:09 -0500279 log<level::ERR>("Invalid IBM logging callout save file. Deleting",
280 entry("FILE=%s", f.path().c_str()));
281 fs::remove(f.path());
282 continue;
283 }
284
285 auto path = getCalloutObjectPath(objectPath, id);
286 auto callout = std::make_shared<Callout>(bus, path, id,
287 getLogTimestamp(interfaces));
288 if (callout->deserialize(saveDir))
289 {
290 callout->emit_object_added();
Matt Spinler54ea3ad2018-10-23 10:40:09 -0500291 std::any anyObject = callout;
Matt Spinler60f53d82018-05-23 13:23:09 -0500292 addChildInterface(objectPath, InterfaceType::CALLOUT, anyObject);
293 }
294 }
295}
296
Matt Spinlere0017eb2018-03-27 11:17:38 -0500297void Manager::interfaceAdded(sdbusplus::message::message& msg)
298{
299 sdbusplus::message::object_path path;
300 DbusInterfaceMap interfaces;
301
302 msg.read(path, interfaces);
303
Matt Spinler259e7272018-03-29 10:57:17 -0500304 // Find the Logging.Entry interface with all of its properties
305 // to pass to create().
Matt Spinlere6a51592018-05-23 12:47:10 -0500306 if (interfaces.find(LOGGING_IFACE) != interfaces.end())
Matt Spinlere0017eb2018-03-27 11:17:38 -0500307 {
Matt Spinlere6a51592018-05-23 12:47:10 -0500308 create(path, interfaces);
Matt Spinlere0017eb2018-03-27 11:17:38 -0500309 }
310}
Matt Spinler055da3b2018-05-09 15:51:20 -0500311
Matt Spinler52ee71b2018-05-23 13:18:55 -0500312uint64_t Manager::getLogTimestamp(const DbusInterfaceMap& interfaces)
313{
314 auto interface = interfaces.find(LOGGING_IFACE);
315 if (interface != interfaces.end())
316 {
317 auto property = interface->second.find("Timestamp");
318 if (property != interface->second.end())
319 {
320 return property->second.get<uint64_t>();
321 }
322 }
323
324 return 0;
325}
326
327fs::path Manager::getSaveDir(EntryID id)
328{
329 return fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id);
330}
331
332fs::path Manager::getCalloutSaveDir(EntryID id)
333{
334 return getSaveDir(id) / "callouts";
335}
336
337std::string Manager::getCalloutObjectPath(const std::string& objectPath,
338 uint32_t calloutNum)
339{
340 return fs::path{objectPath} / "callouts" / std::to_string(calloutNum);
341}
342
Matt Spinler055da3b2018-05-09 15:51:20 -0500343void Manager::interfaceRemoved(sdbusplus::message::message& msg)
344{
345 sdbusplus::message::object_path path;
346 DbusInterfaceList interfaces;
347
348 msg.read(path, interfaces);
349
350 // If the Logging.Entry interface was removed, then remove
351 // our object
352
353 auto i = std::find(interfaces.begin(), interfaces.end(), LOGGING_IFACE);
354
355 if (i != interfaces.end())
356 {
Matt Spinlera1390352018-05-23 10:48:19 -0500357 erase(getEntryID(path));
Matt Spinler055da3b2018-05-09 15:51:20 -0500358 }
359}
Matt Spinler66e07072018-09-12 10:36:14 -0500360} // namespace logging
361} // namespace ibm