blob: 5239818db5595dc4e5efc93a229f83cfedfb7243 [file] [log] [blame]
Andrew Geissler3b025e62019-02-01 10:33:54 -06001#include "processing.hpp"
2
3#include <boost/algorithm/string/predicate.hpp>
Brad Bishop23520882022-05-26 21:39:53 -04004
Andrew Geissler70461892019-02-27 09:57:37 -06005#include <iostream>
Andrew Geissler3b025e62019-02-01 10:33:54 -06006
7bool getWellKnown(
8 const boost::container::flat_map<std::string, std::string>& owners,
9 const std::string& request, std::string& wellKnown)
10{
11 // If it's already a well known name, just return
12 if (!boost::starts_with(request, ":"))
13 {
14 wellKnown = request;
15 return true;
16 }
17
18 auto it = owners.find(request);
19 if (it == owners.end())
20 {
21 return false;
22 }
23 wellKnown = it->second;
24 return true;
25}
Andrew Geissler82815da2019-02-04 12:19:41 -060026
27bool needToIntrospect(const std::string& processName,
Brad Bishopf944a452022-05-05 15:06:46 -040028 const AllowDenyList& allowList,
29 const AllowDenyList& denyList)
Andrew Geissler82815da2019-02-04 12:19:41 -060030{
Brad Bishopf944a452022-05-05 15:06:46 -040031 auto inAllowList =
32 std::find_if(allowList.begin(), allowList.end(),
Andrew Geissler82815da2019-02-04 12:19:41 -060033 [&processName](const auto& prefix) {
34 return boost::starts_with(processName, prefix);
Brad Bishopf944a452022-05-05 15:06:46 -040035 }) != allowList.end();
Andrew Geissler82815da2019-02-04 12:19:41 -060036
37 // This holds full service names, not prefixes
Brad Bishopf944a452022-05-05 15:06:46 -040038 auto inDenyList = denyList.find(processName) != denyList.end();
Andrew Geissler82815da2019-02-04 12:19:41 -060039
Brad Bishopf944a452022-05-05 15:06:46 -040040 return inAllowList && !inDenyList;
Andrew Geissler82815da2019-02-04 12:19:41 -060041}
Andrew Geissler20679262019-02-11 20:20:40 -060042
43void processNameChangeDelete(
44 boost::container::flat_map<std::string, std::string>& nameOwners,
45 const std::string& wellKnown, const std::string& oldOwner,
Brad Bishopa098a372022-05-05 15:19:04 -040046 InterfaceMapType& interfaceMap, AssociationMaps& assocMaps,
Andrew Geissler20679262019-02-11 20:20:40 -060047 sdbusplus::asio::object_server& server)
48{
49 if (boost::starts_with(oldOwner, ":"))
50 {
51 auto it = nameOwners.find(oldOwner);
52 if (it != nameOwners.end())
53 {
54 nameOwners.erase(it);
55 }
56 }
57 // Connection removed
Brad Bishopa098a372022-05-05 15:19:04 -040058 InterfaceMapType::iterator pathIt = interfaceMap.begin();
Andrew Geissler20679262019-02-11 20:20:40 -060059 while (pathIt != interfaceMap.end())
60 {
61 // If an associations interface is being removed,
62 // also need to remove the corresponding associations
63 // objects and properties.
64 auto ifaces = pathIt->second.find(wellKnown);
65 if (ifaces != pathIt->second.end())
66 {
John Wangd0cf9422019-09-17 16:01:34 +080067 auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(),
68 assocDefsInterface);
Andrew Geissler20679262019-02-11 20:20:40 -060069 if (assoc != ifaces->second.end())
70 {
Matt Spinlere2359fb2019-04-05 14:11:33 -050071 removeAssociation(pathIt->first, wellKnown, server, assocMaps);
Andrew Geissler20679262019-02-11 20:20:40 -060072 }
Matt Spinler9c3d2852019-04-08 15:57:19 -050073
74 // Instead of checking if every single path is the endpoint of an
75 // association that needs to be moved to pending, only check when
76 // we own this path as well, which would be because of an
77 // association.
78 if ((pathIt->second.size() == 2) &&
Brad Bishopa02cd542021-10-12 19:12:42 -040079 (pathIt->second.find("xyz.openbmc_project.ObjectMapper") !=
80 pathIt->second.end()))
Matt Spinler9c3d2852019-04-08 15:57:19 -050081 {
82 // Remove the 2 association D-Bus paths and move the
83 // association to pending.
84 moveAssociationToPending(pathIt->first, assocMaps, server);
85 }
Andrew Geissler20679262019-02-11 20:20:40 -060086 }
87 pathIt->second.erase(wellKnown);
88 if (pathIt->second.empty())
89 {
90 // If the last connection to the object is gone,
91 // delete the top level object
92 pathIt = interfaceMap.erase(pathIt);
93 continue;
94 }
95 pathIt++;
96 }
97}
Andrew Geissler70461892019-02-27 09:57:37 -060098
Brad Bishopa098a372022-05-05 15:19:04 -040099void processInterfaceAdded(InterfaceMapType& interfaceMap,
Andrew Geissler70461892019-02-27 09:57:37 -0600100 const sdbusplus::message::object_path& objPath,
101 const InterfacesAdded& intfAdded,
102 const std::string& wellKnown,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500103 AssociationMaps& assocMaps,
Andrew Geissler70461892019-02-27 09:57:37 -0600104 sdbusplus::asio::object_server& server)
105{
106 auto& ifaceList = interfaceMap[objPath.str];
107
108 for (const auto& interfacePair : intfAdded)
109 {
110 ifaceList[wellKnown].emplace(interfacePair.first);
111
John Wangd0cf9422019-09-17 16:01:34 +0800112 if (interfacePair.first == assocDefsInterface)
Andrew Geissler70461892019-02-27 09:57:37 -0600113 {
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500114 const std::variant<std::vector<Association>>* variantAssociations =
115 nullptr;
Andrew Geissler70461892019-02-27 09:57:37 -0600116 for (const auto& interface : interfacePair.second)
117 {
John Wangd0cf9422019-09-17 16:01:34 +0800118 if (interface.first == assocDefsProperty)
Andrew Geissler70461892019-02-27 09:57:37 -0600119 {
120 variantAssociations = &(interface.second);
121 }
122 }
123 if (variantAssociations == nullptr)
124 {
125 std::cerr << "Illegal association found on " << wellKnown
126 << "\n";
127 continue;
128 }
129 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500130 std::get<std::vector<Association>>(*variantAssociations);
Andrew Geissler70461892019-02-27 09:57:37 -0600131 associationChanged(server, associations, objPath.str, wellKnown,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500132 interfaceMap, assocMaps);
Andrew Geissler70461892019-02-27 09:57:37 -0600133 }
134 }
135
136 // To handle the case where an object path is being created
137 // with 2 or more new path segments, check if the parent paths
138 // of this path are already in the interface map, and add them
139 // if they aren't with just the default freedesktop interfaces.
140 // This would be done via introspection if they would have
141 // already existed at startup. While we could also introspect
142 // them now to do the work, we know there aren't any other
143 // interfaces or we would have gotten signals for them as well,
144 // so take a shortcut to speed things up.
145 //
146 // This is all needed so that mapper operations can be done
147 // on the new parent paths.
Brad Bishopa098a372022-05-05 15:19:04 -0400148 using iface_map_iterator = InterfaceMapType::iterator;
Andrew Geissler70461892019-02-27 09:57:37 -0600149 using iface_map_value_type =
150 boost::container::flat_map<std::string,
151 boost::container::flat_set<std::string>>;
152 using name_map_iterator = iface_map_value_type::iterator;
153
154 static const boost::container::flat_set<std::string> defaultIfaces{
155 "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
156 "org.freedesktop.DBus.Properties"};
157
158 std::string parent = objPath.str;
159 auto pos = parent.find_last_of('/');
160
161 while (pos != std::string::npos)
162 {
163 parent = parent.substr(0, pos);
164
165 std::pair<iface_map_iterator, bool> parentEntry =
166 interfaceMap.insert(std::make_pair(parent, iface_map_value_type{}));
167
168 std::pair<name_map_iterator, bool> ifaceEntry =
169 parentEntry.first->second.insert(
170 std::make_pair(wellKnown, defaultIfaces));
171
172 if (!ifaceEntry.second)
173 {
174 // Entry was already there for this name so done.
175 break;
176 }
177
178 pos = parent.find_last_of('/');
179 }
Matt Spinler11401e22019-04-08 13:13:25 -0500180
181 // The new interface might have an association pending
182 checkIfPendingAssociation(objPath.str, interfaceMap, assocMaps, server);
Andrew Geissler70461892019-02-27 09:57:37 -0600183}