blob: 61e6243e2e90687a80412d7c157b43a065e75884 [file] [log] [blame]
Andrew Geissler3b025e62019-02-01 10:33:54 -06001#include "processing.hpp"
2
Brad Bishop18d3d442025-07-29 17:05:56 -04003#include "associations.hpp"
4
Brad Bishop1e94e602022-06-02 19:47:53 -04005#include <algorithm>
6#include <array>
Andrew Geissler70461892019-02-27 09:57:37 -06007#include <iostream>
Brad Bishop86d28802022-07-11 15:49:31 -04008#include <string>
Brad Bishop1e94e602022-06-02 19:47:53 -04009#include <string_view>
Andrew Geissler3b025e62019-02-01 10:33:54 -060010
11bool getWellKnown(
12 const boost::container::flat_map<std::string, std::string>& owners,
13 const std::string& request, std::string& wellKnown)
14{
15 // If it's already a well known name, just return
Brad Bishop86d28802022-07-11 15:49:31 -040016 if (!request.starts_with(":"))
Andrew Geissler3b025e62019-02-01 10:33:54 -060017 {
18 wellKnown = request;
19 return true;
20 }
21
22 auto it = owners.find(request);
23 if (it == owners.end())
24 {
25 return false;
26 }
27 wellKnown = it->second;
28 return true;
29}
Andrew Geissler82815da2019-02-04 12:19:41 -060030
Brad Bishop1e94e602022-06-02 19:47:53 -040031bool needToIntrospect(const std::string& processName)
Andrew Geissler82815da2019-02-04 12:19:41 -060032{
Brad Bishop1e94e602022-06-02 19:47:53 -040033 using namespace std::string_view_literals;
34 static constexpr std::array<std::string_view, 2> skipNamespaces{
35 ":"sv, "org.freedesktop"sv};
Andrew Geissler82815da2019-02-04 12:19:41 -060036
Brad Bishop1e94e602022-06-02 19:47:53 -040037 auto inSkipList = std::find_if(skipNamespaces.begin(), skipNamespaces.end(),
38 [&processName](auto prefix) {
Patrick Williams9052ebd2024-08-16 15:22:16 -040039 return processName.starts_with(prefix);
40 }) != skipNamespaces.end();
Brad Bishop1e94e602022-06-02 19:47:53 -040041 return !(inSkipList || processName.empty());
Andrew Geissler82815da2019-02-04 12:19:41 -060042}
Andrew Geissler20679262019-02-11 20:20:40 -060043
44void processNameChangeDelete(
Kallas, Pawel5b4357d2022-10-12 15:36:37 +020045 boost::asio::io_context& io,
Andrew Geissler20679262019-02-11 20:20:40 -060046 boost::container::flat_map<std::string, std::string>& nameOwners,
47 const std::string& wellKnown, const std::string& oldOwner,
Brad Bishopa098a372022-05-05 15:19:04 -040048 InterfaceMapType& interfaceMap, AssociationMaps& assocMaps,
Andrew Geissler20679262019-02-11 20:20:40 -060049 sdbusplus::asio::object_server& server)
50{
Brad Bishop86d28802022-07-11 15:49:31 -040051 if (oldOwner.starts_with(":"))
Andrew Geissler20679262019-02-11 20:20:40 -060052 {
53 auto it = nameOwners.find(oldOwner);
54 if (it != nameOwners.end())
55 {
56 nameOwners.erase(it);
57 }
58 }
59 // Connection removed
Brad Bishopa098a372022-05-05 15:19:04 -040060 InterfaceMapType::iterator pathIt = interfaceMap.begin();
Andrew Geissler20679262019-02-11 20:20:40 -060061 while (pathIt != interfaceMap.end())
62 {
63 // If an associations interface is being removed,
64 // also need to remove the corresponding associations
65 // objects and properties.
66 auto ifaces = pathIt->second.find(wellKnown);
67 if (ifaces != pathIt->second.end())
68 {
John Wangd0cf9422019-09-17 16:01:34 +080069 auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(),
70 assocDefsInterface);
Andrew Geissler20679262019-02-11 20:20:40 -060071 if (assoc != ifaces->second.end())
72 {
Kallas, Pawel5b4357d2022-10-12 15:36:37 +020073 removeAssociation(io, pathIt->first, wellKnown, server,
74 assocMaps);
Andrew Geissler20679262019-02-11 20:20:40 -060075 }
Matt Spinler9c3d2852019-04-08 15:57:19 -050076
77 // Instead of checking if every single path is the endpoint of an
78 // association that needs to be moved to pending, only check when
79 // we own this path as well, which would be because of an
80 // association.
81 if ((pathIt->second.size() == 2) &&
Brad Bishopa02cd542021-10-12 19:12:42 -040082 (pathIt->second.find("xyz.openbmc_project.ObjectMapper") !=
83 pathIt->second.end()))
Matt Spinler9c3d2852019-04-08 15:57:19 -050084 {
85 // Remove the 2 association D-Bus paths and move the
86 // association to pending.
Kallas, Pawel5b4357d2022-10-12 15:36:37 +020087 moveAssociationToPending(io, pathIt->first, assocMaps, server);
Matt Spinler9c3d2852019-04-08 15:57:19 -050088 }
Andrew Geissler20679262019-02-11 20:20:40 -060089 }
90 pathIt->second.erase(wellKnown);
91 if (pathIt->second.empty())
92 {
93 // If the last connection to the object is gone,
94 // delete the top level object
95 pathIt = interfaceMap.erase(pathIt);
96 continue;
97 }
98 pathIt++;
99 }
100}
Andrew Geissler70461892019-02-27 09:57:37 -0600101
Patrick Williams9052ebd2024-08-16 15:22:16 -0400102void processInterfaceAdded(
103 boost::asio::io_context& io, InterfaceMapType& interfaceMap,
104 const sdbusplus::message::object_path& objPath,
105 const InterfacesAdded& intfAdded, const std::string& wellKnown,
106 AssociationMaps& assocMaps, sdbusplus::asio::object_server& server)
Andrew Geissler70461892019-02-27 09:57:37 -0600107{
108 auto& ifaceList = interfaceMap[objPath.str];
109
110 for (const auto& interfacePair : intfAdded)
111 {
112 ifaceList[wellKnown].emplace(interfacePair.first);
113
John Wangd0cf9422019-09-17 16:01:34 +0800114 if (interfacePair.first == assocDefsInterface)
Andrew Geissler70461892019-02-27 09:57:37 -0600115 {
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500116 const std::variant<std::vector<Association>>* variantAssociations =
117 nullptr;
Andrew Geissler70461892019-02-27 09:57:37 -0600118 for (const auto& interface : interfacePair.second)
119 {
John Wangd0cf9422019-09-17 16:01:34 +0800120 if (interface.first == assocDefsProperty)
Andrew Geissler70461892019-02-27 09:57:37 -0600121 {
122 variantAssociations = &(interface.second);
123 }
124 }
125 if (variantAssociations == nullptr)
126 {
127 std::cerr << "Illegal association found on " << wellKnown
128 << "\n";
129 continue;
130 }
131 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500132 std::get<std::vector<Association>>(*variantAssociations);
Kallas, Pawel5b4357d2022-10-12 15:36:37 +0200133 associationChanged(io, server, associations, objPath.str, wellKnown,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500134 interfaceMap, assocMaps);
Andrew Geissler70461892019-02-27 09:57:37 -0600135 }
136 }
137
138 // To handle the case where an object path is being created
139 // with 2 or more new path segments, check if the parent paths
140 // of this path are already in the interface map, and add them
141 // if they aren't with just the default freedesktop interfaces.
142 // This would be done via introspection if they would have
143 // already existed at startup. While we could also introspect
144 // them now to do the work, we know there aren't any other
145 // interfaces or we would have gotten signals for them as well,
146 // so take a shortcut to speed things up.
147 //
148 // This is all needed so that mapper operations can be done
149 // on the new parent paths.
Brad Bishopa098a372022-05-05 15:19:04 -0400150 using iface_map_iterator = InterfaceMapType::iterator;
Ed Tanousea0e5d22024-11-11 10:00:56 -0800151 using name_map_iterator = ConnectionNames::iterator;
Andrew Geissler70461892019-02-27 09:57:37 -0600152
Andrew Geissler70461892019-02-27 09:57:37 -0600153 std::string parent = objPath.str;
154 auto pos = parent.find_last_of('/');
155
156 while (pos != std::string::npos)
157 {
158 parent = parent.substr(0, pos);
159
160 std::pair<iface_map_iterator, bool> parentEntry =
Ed Tanousea0e5d22024-11-11 10:00:56 -0800161 interfaceMap.emplace(parent, ConnectionNames{});
Andrew Geissler70461892019-02-27 09:57:37 -0600162
163 std::pair<name_map_iterator, bool> ifaceEntry =
Lei YU7cc8d002024-09-24 02:33:16 +0000164 parentEntry.first->second.emplace(wellKnown, InterfaceNames{});
Andrew Geissler70461892019-02-27 09:57:37 -0600165
166 if (!ifaceEntry.second)
167 {
168 // Entry was already there for this name so done.
169 break;
170 }
171
172 pos = parent.find_last_of('/');
173 }
Matt Spinler11401e22019-04-08 13:13:25 -0500174
175 // The new interface might have an association pending
Kallas, Pawel5b4357d2022-10-12 15:36:37 +0200176 checkIfPendingAssociation(io, objPath.str, interfaceMap, assocMaps, server);
Andrew Geissler70461892019-02-27 09:57:37 -0600177}