blob: 27e60ebd0f6934deaebdfddce34a08f1bd409613 [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>
Patrick Venturea680d1e2018-10-14 13:34:26 -070025
Brad Bishop24424982017-01-13 16:37:14 -050026using namespace std::literals::chrono_literals;
27
Brad Bishop49aefb32016-10-19 11:54:14 -040028namespace phosphor
29{
30namespace inventory
31{
32namespace manager
33{
Brad Bishop49aefb32016-10-19 11:54:14 -040034/** @brief Fowrarding signal callback.
35 *
36 * Extracts per-signal specific context and forwards the call to the manager
37 * instance.
38 */
George Liu23314a52022-04-13 18:26:03 +080039auto _signal(sd_bus_message* m, void* data, sd_bus_error* /* e */) noexcept
Brad Bishop49aefb32016-10-19 11:54:14 -040040{
Brad Bishop7b337772017-01-12 16:11:24 -050041 try
42 {
Patrick Williams563306f2022-07-22 19:26:52 -050043 auto msg = sdbusplus::message_t(m);
Brad Bishop7b337772017-01-12 16:11:24 -050044 auto& args = *static_cast<Manager::SigArg*>(data);
Brad Bishop49aefb32016-10-19 11:54:14 -040045 sd_bus_message_ref(m);
Brad Bishop7b337772017-01-12 16:11:24 -050046 auto& mgr = *std::get<0>(args);
Brad Bishop615b2a82018-03-29 10:32:41 -040047 mgr.handleEvent(msg, static_cast<const DbusSignal&>(*std::get<1>(args)),
48 *std::get<2>(args));
Brad Bishop49aefb32016-10-19 11:54:14 -040049 }
Brad Bishop7b337772017-01-12 16:11:24 -050050 catch (const std::exception& e)
51 {
Brad Bishop49aefb32016-10-19 11:54:14 -040052 std::cerr << e.what() << std::endl;
53 }
54
55 return 0;
56}
57
Patrick Williams563306f2022-07-22 19:26:52 -050058Manager::Manager(sdbusplus::bus_t&& bus, const char* root) :
Brad Bishop4627a9c2020-12-06 19:26:57 -050059 ServerObject<ManagerIface>(bus, root), _root(root), _bus(std::move(bus)),
60 _manager(_bus, root),
Matt Spinler852db672019-03-06 13:46:14 -060061#ifdef CREATE_ASSOCIATIONS
Brad Bishop4627a9c2020-12-06 19:26:57 -050062 _associations(_bus),
Matt Spinler852db672019-03-06 13:46:14 -060063#endif
Brad Bishop4627a9c2020-12-06 19:26:57 -050064 _status(ManagerStatus::STARTING)
Brad Bishop49aefb32016-10-19 11:54:14 -040065{
Brad Bishop7b337772017-01-12 16:11:24 -050066 for (auto& group : _events)
Brad Bishop68c80832016-11-29 16:41:32 -050067 {
Brad Bishop615b2a82018-03-29 10:32:41 -040068 for (auto pEvent : std::get<std::vector<EventBasePtr>>(group))
Brad Bishop68c80832016-11-29 16:41:32 -050069 {
Brad Bishop615b2a82018-03-29 10:32:41 -040070 if (pEvent->type != Event::Type::DBUS_SIGNAL)
Brad Bishop7b337772017-01-12 16:11:24 -050071 {
Brad Bishop68c80832016-11-29 16:41:32 -050072 continue;
Brad Bishop7b337772017-01-12 16:11:24 -050073 }
Brad Bishop4f20a3e2016-11-29 15:21:46 -050074
Brad Bishop68c80832016-11-29 16:41:32 -050075 // Create a callback context for this event group.
Brad Bishop615b2a82018-03-29 10:32:41 -040076 auto dbusEvent = static_cast<DbusSignal*>(pEvent.get());
Brad Bishop68c80832016-11-29 16:41:32 -050077
78 // Go ahead and store an iterator pointing at
79 // the event data to avoid lookups later since
80 // additional signal callbacks aren't added
81 // after the manager is constructed.
82 _sigargs.emplace_back(
Brad Bishop615b2a82018-03-29 10:32:41 -040083 std::make_unique<SigArg>(this, dbusEvent, &group));
Brad Bishop68c80832016-11-29 16:41:32 -050084
85 // Register our callback and the context for
86 // each signal event.
Brad Bishop615b2a82018-03-29 10:32:41 -040087 _matches.emplace_back(_bus, dbusEvent->signature, _signal,
88 _sigargs.back().get());
Brad Bishop68c80832016-11-29 16:41:32 -050089 }
Brad Bishop49aefb32016-10-19 11:54:14 -040090 }
91
Deepak Kodihallib28990f2017-08-08 07:19:34 -050092 // Restore any persistent inventory
93 restore();
Brad Bishop49aefb32016-10-19 11:54:14 -040094}
95
96void Manager::shutdown() noexcept
97{
Brad Bishop4627a9c2020-12-06 19:26:57 -050098 _status = ManagerStatus::STOPPING;
Brad Bishop49aefb32016-10-19 11:54:14 -040099}
100
Brad Bishop20c94352020-12-06 20:23:56 -0500101void Manager::run(const char* busname)
Brad Bishop49aefb32016-10-19 11:54:14 -0400102{
Patrick Williams563306f2022-07-22 19:26:52 -0500103 sdbusplus::message_t unusedMsg{nullptr};
Brad Bishop3e4a19a2017-01-21 22:17:09 -0500104
105 // Run startup events.
106 for (auto& group : _events)
107 {
Brad Bishop615b2a82018-03-29 10:32:41 -0400108 for (auto pEvent : std::get<std::vector<EventBasePtr>>(group))
Brad Bishop3e4a19a2017-01-21 22:17:09 -0500109 {
Brad Bishop615b2a82018-03-29 10:32:41 -0400110 if (pEvent->type == Event::Type::STARTUP)
Brad Bishop3e4a19a2017-01-21 22:17:09 -0500111 {
112 handleEvent(unusedMsg, *pEvent, group);
113 }
114 }
115 }
116
Brad Bishop4627a9c2020-12-06 19:26:57 -0500117 _status = ManagerStatus::RUNNING;
Brad Bishop20c94352020-12-06 20:23:56 -0500118 _bus.request_name(busname);
Brad Bishop4627a9c2020-12-06 19:26:57 -0500119
120 while (_status != ManagerStatus::STOPPING)
Brad Bishop7b337772017-01-12 16:11:24 -0500121 {
122 try
123 {
Brad Bishop49aefb32016-10-19 11:54:14 -0400124 _bus.process_discard();
Brad Bishop24424982017-01-13 16:37:14 -0500125 _bus.wait((5000000us).count());
Brad Bishop49aefb32016-10-19 11:54:14 -0400126 }
Brad Bishop7b337772017-01-12 16:11:24 -0500127 catch (const std::exception& e)
128 {
Brad Bishop49aefb32016-10-19 11:54:14 -0400129 std::cerr << e.what() << std::endl;
130 }
131 }
132}
133
Brad Bishop615b2a82018-03-29 10:32:41 -0400134void Manager::updateInterfaces(const sdbusplus::message::object_path& path,
135 const Object& interfaces,
136 ObjectReferences::iterator pos, bool newObject,
137 bool restoreFromCache)
Brad Bishop79ccaf72017-01-22 16:00:50 -0500138{
139 auto& refaces = pos->second;
140 auto ifaceit = interfaces.cbegin();
141 auto opsit = _makers.cbegin();
142 auto refaceit = refaces.begin();
143 std::vector<std::string> signals;
144
145 while (ifaceit != interfaces.cend())
146 {
147 try
148 {
149 // Find the binding ops for this interface.
Brad Bishop615b2a82018-03-29 10:32:41 -0400150 opsit = std::lower_bound(opsit, _makers.cend(), ifaceit->first,
151 compareFirst(_makers.key_comp()));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500152
153 if (opsit == _makers.cend() || opsit->first != ifaceit->first)
154 {
155 // This interface is not supported.
Brad Bishop615b2a82018-03-29 10:32:41 -0400156 throw InterfaceError("Encountered unsupported interface.",
157 ifaceit->first);
Brad Bishop79ccaf72017-01-22 16:00:50 -0500158 }
159
160 // Find the binding insertion point or the binding to update.
Brad Bishop615b2a82018-03-29 10:32:41 -0400161 refaceit = std::lower_bound(refaceit, refaces.end(), ifaceit->first,
162 compareFirst(refaces.key_comp()));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500163
164 if (refaceit == refaces.end() || refaceit->first != ifaceit->first)
165 {
166 // Add the new interface.
Brad Bishop02763c62018-12-12 22:02:07 -0500167 auto& ctor = std::get<MakeInterfaceType>(opsit->second);
Brad Bishop23a8d932020-12-06 19:40:13 -0500168 // skipSignal = true here to avoid getting PropertiesChanged
169 // signals while the interface is constructed. We'll emit an
170 // ObjectManager signal for this interface below.
Brad Bishop79ccaf72017-01-22 16:00:50 -0500171 refaceit = refaces.insert(
Brad Bishope96f2aa2020-12-06 19:09:09 -0500172 refaceit, std::make_pair(ifaceit->first,
173 ctor(_bus, path.str.c_str(),
Brad Bishop23a8d932020-12-06 19:40:13 -0500174 ifaceit->second, true)));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500175 signals.push_back(ifaceit->first);
176 }
177 else
178 {
179 // Set the new property values.
Brad Bishop02763c62018-12-12 22:02:07 -0500180 auto& assign = std::get<AssignInterfaceType>(opsit->second);
Brad Bishop23a8d932020-12-06 19:40:13 -0500181 assign(ifaceit->second, refaceit->second,
182 _status != ManagerStatus::RUNNING);
Brad Bishop79ccaf72017-01-22 16:00:50 -0500183 }
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500184 if (!restoreFromCache)
185 {
Brad Bishop02763c62018-12-12 22:02:07 -0500186 auto& serialize =
187 std::get<SerializeInterfaceType<SerialOps>>(opsit->second);
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500188 serialize(path, ifaceit->first, refaceit->second);
189 }
190 else
191 {
Brad Bishop02763c62018-12-12 22:02:07 -0500192 auto& deserialize =
193 std::get<DeserializeInterfaceType<SerialOps>>(
194 opsit->second);
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500195 deserialize(path, ifaceit->first, refaceit->second);
196 }
Brad Bishop79ccaf72017-01-22 16:00:50 -0500197 }
198 catch (const InterfaceError& e)
199 {
200 // Reset the binding ops iterator since we are
201 // at the end.
202 opsit = _makers.cbegin();
203 e.log();
204 }
205
206 ++ifaceit;
207 }
208
Brad Bishop23a8d932020-12-06 19:40:13 -0500209 if (_status == ManagerStatus::RUNNING)
Brad Bishop79ccaf72017-01-22 16:00:50 -0500210 {
Brad Bishop23a8d932020-12-06 19:40:13 -0500211 if (newObject)
212 {
213 _bus.emit_object_added(path.str.c_str());
214 }
215 else if (!signals.empty())
216 {
217 _bus.emit_interfaces_added(path.str.c_str(), signals);
218 }
Brad Bishop79ccaf72017-01-22 16:00:50 -0500219 }
220}
221
222void Manager::updateObjects(
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500223 const std::map<sdbusplus::message::object_path, Object>& objs,
224 bool restoreFromCache)
Brad Bishop79ccaf72017-01-22 16:00:50 -0500225{
226 auto objit = objs.cbegin();
227 auto refit = _refs.begin();
228 std::string absPath;
229 bool newObj;
230
231 while (objit != objs.cend())
232 {
233 // Find the insertion point or the object to update.
Brad Bishop615b2a82018-03-29 10:32:41 -0400234 refit = std::lower_bound(refit, _refs.end(), objit->first,
235 compareFirst(RelPathCompare(_root)));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500236
237 absPath.assign(_root);
238 absPath.append(objit->first);
239
240 newObj = false;
241 if (refit == _refs.end() || refit->first != absPath)
242 {
243 refit = _refs.insert(
Brad Bishop615b2a82018-03-29 10:32:41 -0400244 refit, std::make_pair(absPath, decltype(_refs)::mapped_type()));
Brad Bishop79ccaf72017-01-22 16:00:50 -0500245 newObj = true;
246 }
247
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500248 updateInterfaces(absPath, objit->second, refit, newObj,
249 restoreFromCache);
Matt Spinler852db672019-03-06 13:46:14 -0600250#ifdef CREATE_ASSOCIATIONS
Matt Spinler59521e82021-02-11 13:41:06 -0600251 if (!_associations.pendingCondition() && newObj)
Matt Spinler852db672019-03-06 13:46:14 -0600252 {
Brad Bishop23a8d932020-12-06 19:40:13 -0500253 _associations.createAssociations(absPath,
Brad Bishopee294d62021-01-13 16:14:29 -0500254 _status != ManagerStatus::RUNNING);
Matt Spinler852db672019-03-06 13:46:14 -0600255 }
Matt Spinler59521e82021-02-11 13:41:06 -0600256 else if (!restoreFromCache &&
257 _associations.conditionMatch(objit->first, objit->second))
258 {
259 // The objit path/interface/property matched a pending condition.
260 // Now the associations are valid so attempt to create them against
261 // all existing objects. If this was the restoreFromCache path,
262 // objit doesn't contain property values so don't bother checking.
263 std::for_each(_refs.begin(), _refs.end(), [this](const auto& ref) {
264 _associations.createAssociations(
265 ref.first, _status != ManagerStatus::RUNNING);
266 });
267 }
Matt Spinler852db672019-03-06 13:46:14 -0600268#endif
Brad Bishop79ccaf72017-01-22 16:00:50 -0500269 ++objit;
270 }
271}
272
Brad Bishop03f4cd92017-02-03 15:17:21 -0500273void Manager::notify(std::map<sdbusplus::message::object_path, Object> objs)
Brad Bishop49aefb32016-10-19 11:54:14 -0400274{
Brad Bishopcda036f2017-02-15 10:06:48 -0500275 updateObjects(objs);
Brad Bishop49aefb32016-10-19 11:54:14 -0400276}
277
Patrick Williams563306f2022-07-22 19:26:52 -0500278void Manager::handleEvent(sdbusplus::message_t& msg, const Event& event,
Brad Bishop615b2a82018-03-29 10:32:41 -0400279 const EventInfo& info)
Brad Bishop49aefb32016-10-19 11:54:14 -0400280{
Brad Bishop7b337772017-01-12 16:11:24 -0500281 auto& actions = std::get<1>(info);
Brad Bishop3d57f502016-10-19 12:18:41 -0400282
Brad Bishop48547a82017-01-19 15:12:50 -0500283 for (auto& f : event)
Brad Bishop7b337772017-01-12 16:11:24 -0500284 {
Brad Bishop07934a62017-02-08 23:34:59 -0500285 if (!f(_bus, msg, *this))
Brad Bishop7b337772017-01-12 16:11:24 -0500286 {
Brad Bishop064c94a2017-01-21 21:33:30 -0500287 return;
Brad Bishop7b337772017-01-12 16:11:24 -0500288 }
Brad Bishop3d57f502016-10-19 12:18:41 -0400289 }
Brad Bishop064c94a2017-01-21 21:33:30 -0500290 for (auto& action : actions)
291 {
Brad Bishop07934a62017-02-08 23:34:59 -0500292 action(_bus, *this);
Brad Bishop064c94a2017-01-21 21:33:30 -0500293 }
Brad Bishop49aefb32016-10-19 11:54:14 -0400294}
295
Brad Bishop615b2a82018-03-29 10:32:41 -0400296void Manager::destroyObjects(const std::vector<const char*>& paths)
Brad Bishop656a7d02016-10-19 22:20:02 -0400297{
Brad Bishopa5cc34c2017-02-03 20:57:36 -0500298 std::string p;
299
Brad Bishop7b7e7122017-01-21 21:21:46 -0500300 for (const auto& path : paths)
301 {
Brad Bishopa5cc34c2017-02-03 20:57:36 -0500302 p.assign(_root);
303 p.append(path);
304 _bus.emit_object_removed(p.c_str());
305 _refs.erase(p);
Brad Bishop7b7e7122017-01-21 21:21:46 -0500306 }
Brad Bishop656a7d02016-10-19 22:20:02 -0400307}
308
Brad Bishopeb68a682017-01-22 00:58:54 -0500309void Manager::createObjects(
310 const std::map<sdbusplus::message::object_path, Object>& objs)
311{
Brad Bishopcda036f2017-02-15 10:06:48 -0500312 updateObjects(objs);
Brad Bishopeb68a682017-01-22 00:58:54 -0500313}
314
Brad Bishop25d54b52018-11-21 12:57:51 -0500315std::any& Manager::getInterfaceHolder(const char* path, const char* interface)
Brad Bishopb83a21e2016-11-30 13:43:37 -0500316{
Brad Bishop25d54b52018-11-21 12:57:51 -0500317 return const_cast<std::any&>(
Brad Bishop615b2a82018-03-29 10:32:41 -0400318 const_cast<const Manager*>(this)->getInterfaceHolder(path, interface));
Brad Bishopb83a21e2016-11-30 13:43:37 -0500319}
320
Brad Bishop25d54b52018-11-21 12:57:51 -0500321const std::any& Manager::getInterfaceHolder(const char* path,
322 const char* interface) const
Brad Bishopb83a21e2016-11-30 13:43:37 -0500323{
324 std::string p{path};
325 auto oit = _refs.find(_root + p);
Brad Bishop7b337772017-01-12 16:11:24 -0500326 if (oit == _refs.end())
Brad Bishop615b2a82018-03-29 10:32:41 -0400327 throw std::runtime_error(_root + p + " was not found");
Brad Bishopb83a21e2016-11-30 13:43:37 -0500328
Brad Bishop7b337772017-01-12 16:11:24 -0500329 auto& obj = oit->second;
Brad Bishopb83a21e2016-11-30 13:43:37 -0500330 auto iit = obj.find(interface);
Brad Bishop7b337772017-01-12 16:11:24 -0500331 if (iit == obj.end())
Brad Bishop615b2a82018-03-29 10:32:41 -0400332 throw std::runtime_error("interface was not found");
Brad Bishopb83a21e2016-11-30 13:43:37 -0500333
Brad Bishop150147a2017-02-08 23:57:46 -0500334 return iit->second;
Brad Bishopb83a21e2016-11-30 13:43:37 -0500335}
336
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500337void Manager::restore()
338{
Kun Yie6b21c72019-03-27 09:53:51 -0700339 namespace fs = std::filesystem;
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500340
341 if (!fs::exists(fs::path(PIM_PERSIST_PATH)))
342 {
343 return;
344 }
345
Patrick Williams7edafe02023-05-10 07:50:28 -0500346 static const std::string remove = std::string(PIM_PERSIST_PATH) +
347 INVENTORY_ROOT;
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500348
349 std::map<sdbusplus::message::object_path, Object> objects;
350 for (const auto& dirent :
351 fs::recursive_directory_iterator(PIM_PERSIST_PATH))
352 {
353 const auto& path = dirent.path();
354 if (fs::is_regular_file(path))
355 {
356 auto ifaceName = path.filename().string();
357 auto objPath = path.parent_path().string();
358 objPath.erase(0, remove.length());
359 auto objit = objects.find(objPath);
360 Interface propertyMap{};
361 if (objects.end() != objit)
362 {
363 auto& object = objit->second;
364 object.emplace(std::move(ifaceName), std::move(propertyMap));
365 }
366 else
367 {
368 Object object;
369 object.emplace(std::move(ifaceName), std::move(propertyMap));
370 objects.emplace(std::move(objPath), std::move(object));
371 }
372 }
373 }
374 if (!objects.empty())
375 {
376 auto restoreFromCache = true;
377 updateObjects(objects, restoreFromCache);
Matt Spinler59521e82021-02-11 13:41:06 -0600378
379#ifdef CREATE_ASSOCIATIONS
380 // There may be conditional associations waiting to be loaded
381 // based on certain path/interface/property values. Now that
382 // _refs contains all objects with their property values, check
383 // which property values the conditions need and set them in the
384 // condition structure entries, using the actualValue field. Then
385 // the associations manager can check if the conditions are met.
386 if (_associations.pendingCondition())
387 {
388 ObjectReferences::iterator refIt;
389 InterfaceComposite::iterator ifaceIt;
390
391 auto& conditions = _associations.getConditions();
392 for (auto& condition : conditions)
393 {
394 refIt = _refs.find(_root + condition.path);
395 if (refIt != _refs.end())
396 {
397 ifaceIt = refIt->second.find(condition.interface);
398 }
399
400 if ((refIt != _refs.end()) && (ifaceIt != refIt->second.end()))
401 {
402 const auto& maker = _makers.find(condition.interface);
403 if (maker != _makers.end())
404 {
405 auto& getProperty =
406 std::get<GetPropertyValueType>(maker->second);
407
Patrick Williams7edafe02023-05-10 07:50:28 -0500408 condition.actualValue = getProperty(condition.property,
409 ifaceIt->second);
Matt Spinler59521e82021-02-11 13:41:06 -0600410 }
411 }
412 }
413
414 // Check if a property value in a condition matches an
415 // actual property value just saved. If one did, now the
416 // associations file is valid so create its associations.
417 if (_associations.conditionMatch())
418 {
Patrick Williams7edafe02023-05-10 07:50:28 -0500419 std::for_each(_refs.begin(), _refs.end(),
420 [this](const auto& ref) {
421 _associations.createAssociations(
422 ref.first, _status != ManagerStatus::RUNNING);
423 });
Matt Spinler59521e82021-02-11 13:41:06 -0600424 }
425 }
426#endif
Deepak Kodihallib28990f2017-08-08 07:19:34 -0500427 }
428}
429
Brad Bishop49aefb32016-10-19 11:54:14 -0400430} // namespace manager
431} // namespace inventory
432} // namespace phosphor
433
434// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4