blob: 561cbde473d12fe35a0c0ba5172d944b9768c538 [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 Bishopd5542322022-06-02 19:56:23 -040028 const AllowDenyList& allowList)
Andrew Geissler82815da2019-02-04 12:19:41 -060029{
Brad Bishopf944a452022-05-05 15:06:46 -040030 auto inAllowList =
31 std::find_if(allowList.begin(), allowList.end(),
Andrew Geissler82815da2019-02-04 12:19:41 -060032 [&processName](const auto& prefix) {
33 return boost::starts_with(processName, prefix);
Brad Bishopf944a452022-05-05 15:06:46 -040034 }) != allowList.end();
Andrew Geissler82815da2019-02-04 12:19:41 -060035
Brad Bishopd5542322022-06-02 19:56:23 -040036 return inAllowList;
Andrew Geissler82815da2019-02-04 12:19:41 -060037}
Andrew Geissler20679262019-02-11 20:20:40 -060038
39void processNameChangeDelete(
40 boost::container::flat_map<std::string, std::string>& nameOwners,
41 const std::string& wellKnown, const std::string& oldOwner,
Brad Bishopa098a372022-05-05 15:19:04 -040042 InterfaceMapType& interfaceMap, AssociationMaps& assocMaps,
Andrew Geissler20679262019-02-11 20:20:40 -060043 sdbusplus::asio::object_server& server)
44{
45 if (boost::starts_with(oldOwner, ":"))
46 {
47 auto it = nameOwners.find(oldOwner);
48 if (it != nameOwners.end())
49 {
50 nameOwners.erase(it);
51 }
52 }
53 // Connection removed
Brad Bishopa098a372022-05-05 15:19:04 -040054 InterfaceMapType::iterator pathIt = interfaceMap.begin();
Andrew Geissler20679262019-02-11 20:20:40 -060055 while (pathIt != interfaceMap.end())
56 {
57 // If an associations interface is being removed,
58 // also need to remove the corresponding associations
59 // objects and properties.
60 auto ifaces = pathIt->second.find(wellKnown);
61 if (ifaces != pathIt->second.end())
62 {
John Wangd0cf9422019-09-17 16:01:34 +080063 auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(),
64 assocDefsInterface);
Andrew Geissler20679262019-02-11 20:20:40 -060065 if (assoc != ifaces->second.end())
66 {
Matt Spinlere2359fb2019-04-05 14:11:33 -050067 removeAssociation(pathIt->first, wellKnown, server, assocMaps);
Andrew Geissler20679262019-02-11 20:20:40 -060068 }
Matt Spinler9c3d2852019-04-08 15:57:19 -050069
70 // Instead of checking if every single path is the endpoint of an
71 // association that needs to be moved to pending, only check when
72 // we own this path as well, which would be because of an
73 // association.
74 if ((pathIt->second.size() == 2) &&
Brad Bishopa02cd542021-10-12 19:12:42 -040075 (pathIt->second.find("xyz.openbmc_project.ObjectMapper") !=
76 pathIt->second.end()))
Matt Spinler9c3d2852019-04-08 15:57:19 -050077 {
78 // Remove the 2 association D-Bus paths and move the
79 // association to pending.
80 moveAssociationToPending(pathIt->first, assocMaps, server);
81 }
Andrew Geissler20679262019-02-11 20:20:40 -060082 }
83 pathIt->second.erase(wellKnown);
84 if (pathIt->second.empty())
85 {
86 // If the last connection to the object is gone,
87 // delete the top level object
88 pathIt = interfaceMap.erase(pathIt);
89 continue;
90 }
91 pathIt++;
92 }
93}
Andrew Geissler70461892019-02-27 09:57:37 -060094
Brad Bishopa098a372022-05-05 15:19:04 -040095void processInterfaceAdded(InterfaceMapType& interfaceMap,
Andrew Geissler70461892019-02-27 09:57:37 -060096 const sdbusplus::message::object_path& objPath,
97 const InterfacesAdded& intfAdded,
98 const std::string& wellKnown,
Matt Spinlere2359fb2019-04-05 14:11:33 -050099 AssociationMaps& assocMaps,
Andrew Geissler70461892019-02-27 09:57:37 -0600100 sdbusplus::asio::object_server& server)
101{
102 auto& ifaceList = interfaceMap[objPath.str];
103
104 for (const auto& interfacePair : intfAdded)
105 {
106 ifaceList[wellKnown].emplace(interfacePair.first);
107
John Wangd0cf9422019-09-17 16:01:34 +0800108 if (interfacePair.first == assocDefsInterface)
Andrew Geissler70461892019-02-27 09:57:37 -0600109 {
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500110 const std::variant<std::vector<Association>>* variantAssociations =
111 nullptr;
Andrew Geissler70461892019-02-27 09:57:37 -0600112 for (const auto& interface : interfacePair.second)
113 {
John Wangd0cf9422019-09-17 16:01:34 +0800114 if (interface.first == assocDefsProperty)
Andrew Geissler70461892019-02-27 09:57:37 -0600115 {
116 variantAssociations = &(interface.second);
117 }
118 }
119 if (variantAssociations == nullptr)
120 {
121 std::cerr << "Illegal association found on " << wellKnown
122 << "\n";
123 continue;
124 }
125 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500126 std::get<std::vector<Association>>(*variantAssociations);
Andrew Geissler70461892019-02-27 09:57:37 -0600127 associationChanged(server, associations, objPath.str, wellKnown,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500128 interfaceMap, assocMaps);
Andrew Geissler70461892019-02-27 09:57:37 -0600129 }
130 }
131
132 // To handle the case where an object path is being created
133 // with 2 or more new path segments, check if the parent paths
134 // of this path are already in the interface map, and add them
135 // if they aren't with just the default freedesktop interfaces.
136 // This would be done via introspection if they would have
137 // already existed at startup. While we could also introspect
138 // them now to do the work, we know there aren't any other
139 // interfaces or we would have gotten signals for them as well,
140 // so take a shortcut to speed things up.
141 //
142 // This is all needed so that mapper operations can be done
143 // on the new parent paths.
Brad Bishopa098a372022-05-05 15:19:04 -0400144 using iface_map_iterator = InterfaceMapType::iterator;
Ed Tanous964681c2022-07-08 12:47:24 -0700145 using name_map_iterator = InterfaceMapType::mapped_type::iterator;
Andrew Geissler70461892019-02-27 09:57:37 -0600146
Ed Tanous964681c2022-07-08 12:47:24 -0700147 static const InterfaceNames defaultIfaces{
Andrew Geissler70461892019-02-27 09:57:37 -0600148 "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
149 "org.freedesktop.DBus.Properties"};
150
151 std::string parent = objPath.str;
152 auto pos = parent.find_last_of('/');
153
154 while (pos != std::string::npos)
155 {
156 parent = parent.substr(0, pos);
157
158 std::pair<iface_map_iterator, bool> parentEntry =
Ed Tanous964681c2022-07-08 12:47:24 -0700159 interfaceMap.try_emplace(parent);
Andrew Geissler70461892019-02-27 09:57:37 -0600160
161 std::pair<name_map_iterator, bool> ifaceEntry =
Ed Tanous964681c2022-07-08 12:47:24 -0700162 parentEntry.first->second.try_emplace(wellKnown, defaultIfaces);
Andrew Geissler70461892019-02-27 09:57:37 -0600163
164 if (!ifaceEntry.second)
165 {
166 // Entry was already there for this name so done.
167 break;
168 }
169
170 pos = parent.find_last_of('/');
171 }
Matt Spinler11401e22019-04-08 13:13:25 -0500172
173 // The new interface might have an association pending
174 checkIfPendingAssociation(objPath.str, interfaceMap, assocMaps, server);
Andrew Geissler70461892019-02-27 09:57:37 -0600175}