blob: f62afc04f648441aed91eeefa435a1afccd50ceb [file] [log] [blame]
Brad Bishop49aefb32016-10-19 11:54:14 -04001/**
2 * Copyright © 2016 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 */
Brad Bishop49aefb32016-10-19 11:54:14 -040016#include "manager.hpp"
Patrick Venturea680d1e2018-10-14 13:34:26 -070017
Brad Bishop79ccaf72017-01-22 16:00:50 -050018#include "errors.hpp"
Brad Bishop49aefb32016-10-19 11:54:14 -040019
Patrick Venturea680d1e2018-10-14 13:34:26 -070020#include <algorithm>
21#include <chrono>
22#include <exception>
Kun Yie6b21c72019-03-27 09:53:51 -070023#include <filesystem>
Patrick Venturea680d1e2018-10-14 13:34:26 -070024#include <iostream>
25#include <phosphor-logging/log.hpp>
26
Brad Bishop24424982017-01-13 16:37:14 -050027using namespace std::literals::chrono_literals;
28
Brad Bishop49aefb32016-10-19 11:54:14 -040029namespace phosphor
30{
31namespace inventory
32{
33namespace manager
34{
Brad Bishop49aefb32016-10-19 11:54:14 -040035/** @brief Fowrarding signal callback.
36 *
37 * Extracts per-signal specific context and forwards the call to the manager
38 * instance.
39 */
Brad Bishop7b337772017-01-12 16:11:24 -050040auto _signal(sd_bus_message* m, void* data, sd_bus_error* e) noexcept
Brad Bishop49aefb32016-10-19 11:54:14 -040041{
Brad Bishop7b337772017-01-12 16:11:24 -050042 try
43 {
Brad Bishop49aefb32016-10-19 11:54:14 -040044 auto msg = sdbusplus::message::message(m);
Brad Bishop7b337772017-01-12 16:11:24 -050045 auto& args = *static_cast<Manager::SigArg*>(data);
Brad Bishop49aefb32016-10-19 11:54:14 -040046 sd_bus_message_ref(m);
Brad Bishop7b337772017-01-12 16:11:24 -050047 auto& mgr = *std::get<0>(args);
Brad Bishop615b2a82018-03-29 10:32:41 -040048 mgr.handleEvent(msg, static_cast<const DbusSignal&>(*std::get<1>(args)),
49 *std::get<2>(args));
Brad Bishop49aefb32016-10-19 11:54:14 -040050 }
Brad Bishop7b337772017-01-12 16:11:24 -050051 catch (const std::exception& e)
52 {
Brad Bishop49aefb32016-10-19 11:54:14 -040053 std::cerr << e.what() << std::endl;
54 }
55
56 return 0;
57}
58
Brad Bishop615b2a82018-03-29 10:32:41 -040059Manager::Manager(sdbusplus::bus::bus&& bus, const char* busname,
60 const char* root, const char* iface) :
Brad Bishop12f8a3c2017-02-09 00:02:00 -050061 ServerObject<ManagerIface>(bus, root),
Brad Bishop615b2a82018-03-29 10:32:41 -040062 _shutdown(false), _root(root), _bus(std::move(bus)), _manager(_bus, root)
Matt Spinler852db672019-03-06 13:46:14 -060063#ifdef CREATE_ASSOCIATIONS
64 ,
65 _associations(_bus)
66#endif
Brad Bishop49aefb32016-10-19 11:54:14 -040067{
Brad Bishop7b337772017-01-12 16:11:24 -050068 for (auto& group : _events)
Brad Bishop68c80832016-11-29 16:41:32 -050069 {
Brad Bishop615b2a82018-03-29 10:32:41 -040070 for (auto pEvent : std::get<std::vector<EventBasePtr>>(group))
Brad Bishop68c80832016-11-29 16:41:32 -050071 {
Brad Bishop615b2a82018-03-29 10:32:41 -040072 if (pEvent->type != Event::Type::DBUS_SIGNAL)
Brad Bishop7b337772017-01-12 16:11:24 -050073 {
Brad Bishop68c80832016-11-29 16:41:32 -050074 continue;
Brad Bishop7b337772017-01-12 16:11:24 -050075 }
Brad Bishop4f20a3e2016-11-29 15:21:46 -050076
Brad Bishop68c80832016-11-29 16:41:32 -050077 // Create a callback context for this event group.
Brad Bishop615b2a82018-03-29 10:32:41 -040078 auto dbusEvent = static_cast<DbusSignal*>(pEvent.get());
Brad Bishop68c80832016-11-29 16:41:32 -050079
80 // Go ahead and store an iterator pointing at
81 // the event data to avoid lookups later since
82 // additional signal callbacks aren't added
83 // after the manager is constructed.
84 _sigargs.emplace_back(
Brad Bishop615b2a82018-03-29 10:32:41 -040085 std::make_unique<SigArg>(this, dbusEvent, &group));
Brad Bishop68c80832016-11-29 16:41:32 -050086
87 // Register our callback and the context for
88 // each signal event.
Brad Bishop615b2a82018-03-29 10:32:41 -040089 _matches.emplace_back(_bus, dbusEvent->signature, _signal,
90 _sigargs.back().get());
Brad Bishop68c80832016-11-29 16:41:32 -050091 }
Brad Bishop49aefb32016-10-19 11:54:14 -040092 }
93
Deepak Kodihallib28990f2017-08-08 07:19:34 -050094 // Restore any persistent inventory
95 restore();
Andrew Geissler5a71b4c2019-11-12 16:07:09 -060096
97 _bus.request_name(busname);
Brad Bishop49aefb32016-10-19 11:54:14 -040098}
99
100void Manager::shutdown() noexcept
101{
102 _shutdown = true;
103}
104
105void Manager::run() noexcept
106{
Brad Bishop3e4a19a2017-01-21 22:17:09 -0500107 sdbusplus::message::message unusedMsg{nullptr};
108
109 // Run startup events.
110 for (auto& group : _events)
111 {
Brad Bishop615b2a82018-03-29 10:32:41 -0400112 for (auto pEvent : std::get<std::vector<EventBasePtr>>(group))
Brad Bishop3e4a19a2017-01-21 22:17:09 -0500113 {
Brad Bishop615b2a82018-03-29 10:32:41 -0400114 if (pEvent->type == Event::Type::STARTUP)
Brad Bishop3e4a19a2017-01-21 22:17:09 -0500115 {
116 handleEvent(unusedMsg, *pEvent, group);
117 }
118 }
119 }
120
Brad Bishop7b337772017-01-12 16:11:24 -0500121 while (!_shutdown)
122 {
123 try
124 {
Brad Bishop49aefb32016-10-19 11:54:14 -0400125 _bus.process_discard();
Brad Bishop24424982017-01-13 16:37:14 -0500126 _bus.wait((5000000us).count());
Brad Bishop49aefb32016-10-19 11:54:14 -0400127 }
Brad Bishop7b337772017-01-12 16:11:24 -0500128 catch (const std::exception& e)
129 {
Brad Bishop49aefb32016-10-19 11:54:14 -0400130 std::cerr << e.what() << std::endl;
131 }
132 }
133}
134
Brad Bishop615b2a82018-03-29 10:32:41 -0400135void Manager::updateInterfaces(const sdbusplus::message::object_path& path,
136 const Object& interfaces,
137 ObjectReferences::iterator pos, bool newObject,
138 bool restoreFromCache)
Brad Bishop79ccaf72017-01-22 16:00:50 -0500139{
140 auto& refaces = pos->second;
141 auto ifaceit = interfaces.cbegin();
142 auto opsit = _makers.cbegin();
143 auto refaceit = refaces.begin();
144 std::vector<std::string> signals;
145
146 while (ifaceit != interfaces.cend())
147 {
148 try
149 {
150 // Find the binding ops for this interface.
Brad Bishop615b2a82018-03-29 10:32:41 -0400151 opsit = std::lower_bound(opsit, _makers.cend(), ifaceit->first,
152 compareFirst(_makers.key_comp()));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500153
154 if (opsit == _makers.cend() || opsit->first != ifaceit->first)
155 {
156 // This interface is not supported.
Brad Bishop615b2a82018-03-29 10:32:41 -0400157 throw InterfaceError("Encountered unsupported interface.",
158 ifaceit->first);
Brad Bishop79ccaf72017-01-22 16:00:50 -0500159 }
160
161 // Find the binding insertion point or the binding to update.
Brad Bishop615b2a82018-03-29 10:32:41 -0400162 refaceit = std::lower_bound(refaceit, refaces.end(), ifaceit->first,
163 compareFirst(refaces.key_comp()));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500164
165 if (refaceit == refaces.end() || refaceit->first != ifaceit->first)
166 {
167 // Add the new interface.
Brad Bishop02763c62018-12-12 22:02:07 -0500168 auto& ctor = std::get<MakeInterfaceType>(opsit->second);
Brad Bishop79ccaf72017-01-22 16:00:50 -0500169 refaceit = refaces.insert(
Brad Bishop615b2a82018-03-29 10:32:41 -0400170 refaceit,
171 std::make_pair(ifaceit->first, ctor(_bus, path.str.c_str(),
172 ifaceit->second)));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500173 signals.push_back(ifaceit->first);
174 }
175 else
176 {
177 // Set the new property values.
Brad Bishop02763c62018-12-12 22:02:07 -0500178 auto& assign = std::get<AssignInterfaceType>(opsit->second);
Brad Bishop79ccaf72017-01-22 16:00:50 -0500179 assign(ifaceit->second, refaceit->second);
180 }
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500181 if (!restoreFromCache)
182 {
Brad Bishop02763c62018-12-12 22:02:07 -0500183 auto& serialize =
184 std::get<SerializeInterfaceType<SerialOps>>(opsit->second);
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500185 serialize(path, ifaceit->first, refaceit->second);
186 }
187 else
188 {
Brad Bishop02763c62018-12-12 22:02:07 -0500189 auto& deserialize =
190 std::get<DeserializeInterfaceType<SerialOps>>(
191 opsit->second);
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500192 deserialize(path, ifaceit->first, refaceit->second);
193 }
Brad Bishop79ccaf72017-01-22 16:00:50 -0500194 }
195 catch (const InterfaceError& e)
196 {
197 // Reset the binding ops iterator since we are
198 // at the end.
199 opsit = _makers.cbegin();
200 e.log();
201 }
202
203 ++ifaceit;
204 }
205
206 if (newObject)
207 {
208 _bus.emit_object_added(path.str.c_str());
209 }
210 else if (!signals.empty())
211 {
Gunnar Millsa8ff8152017-06-06 12:54:28 -0500212 _bus.emit_interfaces_added(path.str.c_str(), signals);
Brad Bishop79ccaf72017-01-22 16:00:50 -0500213 }
214}
215
216void Manager::updateObjects(
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500217 const std::map<sdbusplus::message::object_path, Object>& objs,
218 bool restoreFromCache)
Brad Bishop79ccaf72017-01-22 16:00:50 -0500219{
220 auto objit = objs.cbegin();
221 auto refit = _refs.begin();
222 std::string absPath;
223 bool newObj;
224
225 while (objit != objs.cend())
226 {
227 // Find the insertion point or the object to update.
Brad Bishop615b2a82018-03-29 10:32:41 -0400228 refit = std::lower_bound(refit, _refs.end(), objit->first,
229 compareFirst(RelPathCompare(_root)));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500230
231 absPath.assign(_root);
232 absPath.append(objit->first);
233
234 newObj = false;
235 if (refit == _refs.end() || refit->first != absPath)
236 {
237 refit = _refs.insert(
Brad Bishop615b2a82018-03-29 10:32:41 -0400238 refit, std::make_pair(absPath, decltype(_refs)::mapped_type()));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500239 newObj = true;
240 }
241
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500242 updateInterfaces(absPath, objit->second, refit, newObj,
243 restoreFromCache);
Matt Spinler852db672019-03-06 13:46:14 -0600244#ifdef CREATE_ASSOCIATIONS
245 if (newObj)
246 {
247 _associations.createAssociations(absPath);
248 }
249#endif
Brad Bishop79ccaf72017-01-22 16:00:50 -0500250 ++objit;
251 }
252}
253
Brad Bishop03f4cd92017-02-03 15:17:21 -0500254void Manager::notify(std::map<sdbusplus::message::object_path, Object> objs)
Brad Bishop49aefb32016-10-19 11:54:14 -0400255{
Brad Bishopcda036f2017-02-15 10:06:48 -0500256 updateObjects(objs);
Brad Bishop49aefb32016-10-19 11:54:14 -0400257}
258
Brad Bishop615b2a82018-03-29 10:32:41 -0400259void Manager::handleEvent(sdbusplus::message::message& msg, const Event& event,
260 const EventInfo& info)
Brad Bishop49aefb32016-10-19 11:54:14 -0400261{
Brad Bishop7b337772017-01-12 16:11:24 -0500262 auto& actions = std::get<1>(info);
Brad Bishop3d57f502016-10-19 12:18:41 -0400263
Brad Bishop48547a82017-01-19 15:12:50 -0500264 for (auto& f : event)
Brad Bishop7b337772017-01-12 16:11:24 -0500265 {
Brad Bishop07934a62017-02-08 23:34:59 -0500266 if (!f(_bus, msg, *this))
Brad Bishop7b337772017-01-12 16:11:24 -0500267 {
Brad Bishop064c94a2017-01-21 21:33:30 -0500268 return;
Brad Bishop7b337772017-01-12 16:11:24 -0500269 }
Brad Bishop3d57f502016-10-19 12:18:41 -0400270 }
Brad Bishop064c94a2017-01-21 21:33:30 -0500271 for (auto& action : actions)
272 {
Brad Bishop07934a62017-02-08 23:34:59 -0500273 action(_bus, *this);
Brad Bishop064c94a2017-01-21 21:33:30 -0500274 }
Brad Bishop49aefb32016-10-19 11:54:14 -0400275}
276
Brad Bishop615b2a82018-03-29 10:32:41 -0400277void Manager::destroyObjects(const std::vector<const char*>& paths)
Brad Bishop656a7d02016-10-19 22:20:02 -0400278{
Brad Bishopa5cc34c2017-02-03 20:57:36 -0500279 std::string p;
280
Brad Bishop7b7e7122017-01-21 21:21:46 -0500281 for (const auto& path : paths)
282 {
Brad Bishopa5cc34c2017-02-03 20:57:36 -0500283 p.assign(_root);
284 p.append(path);
285 _bus.emit_object_removed(p.c_str());
286 _refs.erase(p);
Brad Bishop7b7e7122017-01-21 21:21:46 -0500287 }
Brad Bishop656a7d02016-10-19 22:20:02 -0400288}
289
Brad Bishopeb68a682017-01-22 00:58:54 -0500290void Manager::createObjects(
291 const std::map<sdbusplus::message::object_path, Object>& objs)
292{
Brad Bishopcda036f2017-02-15 10:06:48 -0500293 updateObjects(objs);
Brad Bishopeb68a682017-01-22 00:58:54 -0500294}
295
Brad Bishop25d54b52018-11-21 12:57:51 -0500296std::any& Manager::getInterfaceHolder(const char* path, const char* interface)
Brad Bishopb83a21e2016-11-30 13:43:37 -0500297{
Brad Bishop25d54b52018-11-21 12:57:51 -0500298 return const_cast<std::any&>(
Brad Bishop615b2a82018-03-29 10:32:41 -0400299 const_cast<const Manager*>(this)->getInterfaceHolder(path, interface));
Brad Bishopb83a21e2016-11-30 13:43:37 -0500300}
301
Brad Bishop25d54b52018-11-21 12:57:51 -0500302const std::any& Manager::getInterfaceHolder(const char* path,
303 const char* interface) const
Brad Bishopb83a21e2016-11-30 13:43:37 -0500304{
305 std::string p{path};
306 auto oit = _refs.find(_root + p);
Brad Bishop7b337772017-01-12 16:11:24 -0500307 if (oit == _refs.end())
Brad Bishop615b2a82018-03-29 10:32:41 -0400308 throw std::runtime_error(_root + p + " was not found");
Brad Bishopb83a21e2016-11-30 13:43:37 -0500309
Brad Bishop7b337772017-01-12 16:11:24 -0500310 auto& obj = oit->second;
Brad Bishopb83a21e2016-11-30 13:43:37 -0500311 auto iit = obj.find(interface);
Brad Bishop7b337772017-01-12 16:11:24 -0500312 if (iit == obj.end())
Brad Bishop615b2a82018-03-29 10:32:41 -0400313 throw std::runtime_error("interface was not found");
Brad Bishopb83a21e2016-11-30 13:43:37 -0500314
Brad Bishop150147a2017-02-08 23:57:46 -0500315 return iit->second;
Brad Bishopb83a21e2016-11-30 13:43:37 -0500316}
317
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500318void Manager::restore()
319{
Kun Yie6b21c72019-03-27 09:53:51 -0700320 namespace fs = std::filesystem;
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500321
322 if (!fs::exists(fs::path(PIM_PERSIST_PATH)))
323 {
324 return;
325 }
326
327 static const std::string remove =
328 std::string(PIM_PERSIST_PATH) + INVENTORY_ROOT;
329
330 std::map<sdbusplus::message::object_path, Object> objects;
331 for (const auto& dirent :
332 fs::recursive_directory_iterator(PIM_PERSIST_PATH))
333 {
334 const auto& path = dirent.path();
335 if (fs::is_regular_file(path))
336 {
337 auto ifaceName = path.filename().string();
338 auto objPath = path.parent_path().string();
339 objPath.erase(0, remove.length());
340 auto objit = objects.find(objPath);
341 Interface propertyMap{};
342 if (objects.end() != objit)
343 {
344 auto& object = objit->second;
345 object.emplace(std::move(ifaceName), std::move(propertyMap));
346 }
347 else
348 {
349 Object object;
350 object.emplace(std::move(ifaceName), std::move(propertyMap));
351 objects.emplace(std::move(objPath), std::move(object));
352 }
353 }
354 }
355 if (!objects.empty())
356 {
357 auto restoreFromCache = true;
358 updateObjects(objects, restoreFromCache);
359 }
360}
361
Brad Bishop49aefb32016-10-19 11:54:14 -0400362} // namespace manager
363} // namespace inventory
364} // namespace phosphor
365
366// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4