blob: f144b77426286efb31b651b01bd41c6f91288562 [file] [log] [blame]
Andrew Geissler3b025e62019-02-01 10:33:54 -06001#include "processing.hpp"
2
3#include <boost/algorithm/string/predicate.hpp>
Andrew Geissler70461892019-02-27 09:57:37 -06004#include <iostream>
Andrew Geissler3b025e62019-02-01 10:33:54 -06005
6bool getWellKnown(
7 const boost::container::flat_map<std::string, std::string>& owners,
8 const std::string& request, std::string& wellKnown)
9{
10 // If it's already a well known name, just return
11 if (!boost::starts_with(request, ":"))
12 {
13 wellKnown = request;
14 return true;
15 }
16
17 auto it = owners.find(request);
18 if (it == owners.end())
19 {
20 return false;
21 }
22 wellKnown = it->second;
23 return true;
24}
Andrew Geissler82815da2019-02-04 12:19:41 -060025
26bool needToIntrospect(const std::string& processName,
27 const WhiteBlackList& whiteList,
28 const WhiteBlackList& blackList)
29{
30 auto inWhitelist =
31 std::find_if(whiteList.begin(), whiteList.end(),
32 [&processName](const auto& prefix) {
33 return boost::starts_with(processName, prefix);
34 }) != whiteList.end();
35
36 // This holds full service names, not prefixes
37 auto inBlacklist = blackList.find(processName) != blackList.end();
38
39 return inWhitelist && !inBlacklist;
40}
Andrew Geissler20679262019-02-11 20:20:40 -060041
42void processNameChangeDelete(
43 boost::container::flat_map<std::string, std::string>& nameOwners,
44 const std::string& wellKnown, const std::string& oldOwner,
Matt Spinlere2359fb2019-04-05 14:11:33 -050045 interface_map_type& interfaceMap, AssociationMaps& assocMaps,
Andrew Geissler20679262019-02-11 20:20:40 -060046 sdbusplus::asio::object_server& server)
47{
48 if (boost::starts_with(oldOwner, ":"))
49 {
50 auto it = nameOwners.find(oldOwner);
51 if (it != nameOwners.end())
52 {
53 nameOwners.erase(it);
54 }
55 }
56 // Connection removed
57 interface_map_type::iterator pathIt = interfaceMap.begin();
58 while (pathIt != interfaceMap.end())
59 {
60 // If an associations interface is being removed,
61 // also need to remove the corresponding associations
62 // objects and properties.
63 auto ifaces = pathIt->second.find(wellKnown);
64 if (ifaces != pathIt->second.end())
65 {
John Wangd0cf9422019-09-17 16:01:34 +080066 auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(),
67 assocDefsInterface);
Andrew Geissler20679262019-02-11 20:20:40 -060068 if (assoc != ifaces->second.end())
69 {
Matt Spinlere2359fb2019-04-05 14:11:33 -050070 removeAssociation(pathIt->first, wellKnown, server, assocMaps);
Andrew Geissler20679262019-02-11 20:20:40 -060071 }
Matt Spinler9c3d2852019-04-08 15:57:19 -050072
73 // Instead of checking if every single path is the endpoint of an
74 // association that needs to be moved to pending, only check when
75 // we own this path as well, which would be because of an
76 // association.
77 if ((pathIt->second.size() == 2) &&
Brad Bishopa02cd542021-10-12 19:12:42 -040078 (pathIt->second.find("xyz.openbmc_project.ObjectMapper") !=
79 pathIt->second.end()))
Matt Spinler9c3d2852019-04-08 15:57:19 -050080 {
81 // Remove the 2 association D-Bus paths and move the
82 // association to pending.
83 moveAssociationToPending(pathIt->first, assocMaps, server);
84 }
Andrew Geissler20679262019-02-11 20:20:40 -060085 }
86 pathIt->second.erase(wellKnown);
87 if (pathIt->second.empty())
88 {
89 // If the last connection to the object is gone,
90 // delete the top level object
91 pathIt = interfaceMap.erase(pathIt);
92 continue;
93 }
94 pathIt++;
95 }
96}
Andrew Geissler70461892019-02-27 09:57:37 -060097
98void processInterfaceAdded(interface_map_type& interfaceMap,
99 const sdbusplus::message::object_path& objPath,
100 const InterfacesAdded& intfAdded,
101 const std::string& wellKnown,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500102 AssociationMaps& assocMaps,
Andrew Geissler70461892019-02-27 09:57:37 -0600103 sdbusplus::asio::object_server& server)
104{
105 auto& ifaceList = interfaceMap[objPath.str];
106
107 for (const auto& interfacePair : intfAdded)
108 {
109 ifaceList[wellKnown].emplace(interfacePair.first);
110
John Wangd0cf9422019-09-17 16:01:34 +0800111 if (interfacePair.first == assocDefsInterface)
Andrew Geissler70461892019-02-27 09:57:37 -0600112 {
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500113 const std::variant<std::vector<Association>>* variantAssociations =
114 nullptr;
Andrew Geissler70461892019-02-27 09:57:37 -0600115 for (const auto& interface : interfacePair.second)
116 {
John Wangd0cf9422019-09-17 16:01:34 +0800117 if (interface.first == assocDefsProperty)
Andrew Geissler70461892019-02-27 09:57:37 -0600118 {
119 variantAssociations = &(interface.second);
120 }
121 }
122 if (variantAssociations == nullptr)
123 {
124 std::cerr << "Illegal association found on " << wellKnown
125 << "\n";
126 continue;
127 }
128 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500129 std::get<std::vector<Association>>(*variantAssociations);
Andrew Geissler70461892019-02-27 09:57:37 -0600130 associationChanged(server, associations, objPath.str, wellKnown,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500131 interfaceMap, assocMaps);
Andrew Geissler70461892019-02-27 09:57:37 -0600132 }
133 }
134
135 // To handle the case where an object path is being created
136 // with 2 or more new path segments, check if the parent paths
137 // of this path are already in the interface map, and add them
138 // if they aren't with just the default freedesktop interfaces.
139 // This would be done via introspection if they would have
140 // already existed at startup. While we could also introspect
141 // them now to do the work, we know there aren't any other
142 // interfaces or we would have gotten signals for them as well,
143 // so take a shortcut to speed things up.
144 //
145 // This is all needed so that mapper operations can be done
146 // on the new parent paths.
147 using iface_map_iterator = interface_map_type::iterator;
148 using iface_map_value_type =
149 boost::container::flat_map<std::string,
150 boost::container::flat_set<std::string>>;
151 using name_map_iterator = iface_map_value_type::iterator;
152
153 static const boost::container::flat_set<std::string> defaultIfaces{
154 "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
155 "org.freedesktop.DBus.Properties"};
156
157 std::string parent = objPath.str;
158 auto pos = parent.find_last_of('/');
159
160 while (pos != std::string::npos)
161 {
162 parent = parent.substr(0, pos);
163
164 std::pair<iface_map_iterator, bool> parentEntry =
165 interfaceMap.insert(std::make_pair(parent, iface_map_value_type{}));
166
167 std::pair<name_map_iterator, bool> ifaceEntry =
168 parentEntry.first->second.insert(
169 std::make_pair(wellKnown, defaultIfaces));
170
171 if (!ifaceEntry.second)
172 {
173 // Entry was already there for this name so done.
174 break;
175 }
176
177 pos = parent.find_last_of('/');
178 }
Matt Spinler11401e22019-04-08 13:13:25 -0500179
180 // The new interface might have an association pending
181 checkIfPendingAssociation(objPath.str, interfaceMap, assocMaps, server);
Andrew Geissler70461892019-02-27 09:57:37 -0600182}