blob: 5a733d4b958074dd5fe4c629fdb8852109a56bf0 [file] [log] [blame]
Andrew Geissler3b025e62019-02-01 10:33:54 -06001#include "processing.hpp"
2
Andrew Geissler70461892019-02-27 09:57:37 -06003#include <iostream>
Brad Bishop86d28802022-07-11 15:49:31 -04004#include <string>
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
Brad Bishop86d28802022-07-11 15:49:31 -040011 if (!request.starts_with(":"))
Andrew Geissler3b025e62019-02-01 10:33:54 -060012 {
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,
Brad Bishopd5542322022-06-02 19:56:23 -040027 const AllowDenyList& allowList)
Andrew Geissler82815da2019-02-04 12:19:41 -060028{
Brad Bishop86d28802022-07-11 15:49:31 -040029 auto inAllowList = std::find_if(allowList.begin(), allowList.end(),
30 [&processName](const auto& prefix) {
31 return processName.starts_with(prefix);
32 }) != allowList.end();
Andrew Geissler82815da2019-02-04 12:19:41 -060033
Brad Bishopd5542322022-06-02 19:56:23 -040034 return inAllowList;
Andrew Geissler82815da2019-02-04 12:19:41 -060035}
Andrew Geissler20679262019-02-11 20:20:40 -060036
37void processNameChangeDelete(
38 boost::container::flat_map<std::string, std::string>& nameOwners,
39 const std::string& wellKnown, const std::string& oldOwner,
Brad Bishopa098a372022-05-05 15:19:04 -040040 InterfaceMapType& interfaceMap, AssociationMaps& assocMaps,
Andrew Geissler20679262019-02-11 20:20:40 -060041 sdbusplus::asio::object_server& server)
42{
Brad Bishop86d28802022-07-11 15:49:31 -040043 if (oldOwner.starts_with(":"))
Andrew Geissler20679262019-02-11 20:20:40 -060044 {
45 auto it = nameOwners.find(oldOwner);
46 if (it != nameOwners.end())
47 {
48 nameOwners.erase(it);
49 }
50 }
51 // Connection removed
Brad Bishopa098a372022-05-05 15:19:04 -040052 InterfaceMapType::iterator pathIt = interfaceMap.begin();
Andrew Geissler20679262019-02-11 20:20:40 -060053 while (pathIt != interfaceMap.end())
54 {
55 // If an associations interface is being removed,
56 // also need to remove the corresponding associations
57 // objects and properties.
58 auto ifaces = pathIt->second.find(wellKnown);
59 if (ifaces != pathIt->second.end())
60 {
John Wangd0cf9422019-09-17 16:01:34 +080061 auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(),
62 assocDefsInterface);
Andrew Geissler20679262019-02-11 20:20:40 -060063 if (assoc != ifaces->second.end())
64 {
Matt Spinlere2359fb2019-04-05 14:11:33 -050065 removeAssociation(pathIt->first, wellKnown, server, assocMaps);
Andrew Geissler20679262019-02-11 20:20:40 -060066 }
Matt Spinler9c3d2852019-04-08 15:57:19 -050067
68 // Instead of checking if every single path is the endpoint of an
69 // association that needs to be moved to pending, only check when
70 // we own this path as well, which would be because of an
71 // association.
72 if ((pathIt->second.size() == 2) &&
Brad Bishopa02cd542021-10-12 19:12:42 -040073 (pathIt->second.find("xyz.openbmc_project.ObjectMapper") !=
74 pathIt->second.end()))
Matt Spinler9c3d2852019-04-08 15:57:19 -050075 {
76 // Remove the 2 association D-Bus paths and move the
77 // association to pending.
78 moveAssociationToPending(pathIt->first, assocMaps, server);
79 }
Andrew Geissler20679262019-02-11 20:20:40 -060080 }
81 pathIt->second.erase(wellKnown);
82 if (pathIt->second.empty())
83 {
84 // If the last connection to the object is gone,
85 // delete the top level object
86 pathIt = interfaceMap.erase(pathIt);
87 continue;
88 }
89 pathIt++;
90 }
91}
Andrew Geissler70461892019-02-27 09:57:37 -060092
Brad Bishopa098a372022-05-05 15:19:04 -040093void processInterfaceAdded(InterfaceMapType& interfaceMap,
Andrew Geissler70461892019-02-27 09:57:37 -060094 const sdbusplus::message::object_path& objPath,
95 const InterfacesAdded& intfAdded,
96 const std::string& wellKnown,
Matt Spinlere2359fb2019-04-05 14:11:33 -050097 AssociationMaps& assocMaps,
Andrew Geissler70461892019-02-27 09:57:37 -060098 sdbusplus::asio::object_server& server)
99{
100 auto& ifaceList = interfaceMap[objPath.str];
101
102 for (const auto& interfacePair : intfAdded)
103 {
104 ifaceList[wellKnown].emplace(interfacePair.first);
105
John Wangd0cf9422019-09-17 16:01:34 +0800106 if (interfacePair.first == assocDefsInterface)
Andrew Geissler70461892019-02-27 09:57:37 -0600107 {
Patrick Williams2bb2d6b2020-05-13 17:59:02 -0500108 const std::variant<std::vector<Association>>* variantAssociations =
109 nullptr;
Andrew Geissler70461892019-02-27 09:57:37 -0600110 for (const auto& interface : interfacePair.second)
111 {
John Wangd0cf9422019-09-17 16:01:34 +0800112 if (interface.first == assocDefsProperty)
Andrew Geissler70461892019-02-27 09:57:37 -0600113 {
114 variantAssociations = &(interface.second);
115 }
116 }
117 if (variantAssociations == nullptr)
118 {
119 std::cerr << "Illegal association found on " << wellKnown
120 << "\n";
121 continue;
122 }
123 std::vector<Association> associations =
Patrick Williamsb05bc122020-05-13 12:21:00 -0500124 std::get<std::vector<Association>>(*variantAssociations);
Andrew Geissler70461892019-02-27 09:57:37 -0600125 associationChanged(server, associations, objPath.str, wellKnown,
Matt Spinlere0b0e3a2019-04-08 10:39:23 -0500126 interfaceMap, assocMaps);
Andrew Geissler70461892019-02-27 09:57:37 -0600127 }
128 }
129
130 // To handle the case where an object path is being created
131 // with 2 or more new path segments, check if the parent paths
132 // of this path are already in the interface map, and add them
133 // if they aren't with just the default freedesktop interfaces.
134 // This would be done via introspection if they would have
135 // already existed at startup. While we could also introspect
136 // them now to do the work, we know there aren't any other
137 // interfaces or we would have gotten signals for them as well,
138 // so take a shortcut to speed things up.
139 //
140 // This is all needed so that mapper operations can be done
141 // on the new parent paths.
Brad Bishopa098a372022-05-05 15:19:04 -0400142 using iface_map_iterator = InterfaceMapType::iterator;
Ed Tanous964681c2022-07-08 12:47:24 -0700143 using name_map_iterator = InterfaceMapType::mapped_type::iterator;
Andrew Geissler70461892019-02-27 09:57:37 -0600144
Ed Tanous964681c2022-07-08 12:47:24 -0700145 static const InterfaceNames defaultIfaces{
Andrew Geissler70461892019-02-27 09:57:37 -0600146 "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
147 "org.freedesktop.DBus.Properties"};
148
149 std::string parent = objPath.str;
150 auto pos = parent.find_last_of('/');
151
152 while (pos != std::string::npos)
153 {
154 parent = parent.substr(0, pos);
155
156 std::pair<iface_map_iterator, bool> parentEntry =
Ed Tanous964681c2022-07-08 12:47:24 -0700157 interfaceMap.try_emplace(parent);
Andrew Geissler70461892019-02-27 09:57:37 -0600158
159 std::pair<name_map_iterator, bool> ifaceEntry =
Ed Tanous964681c2022-07-08 12:47:24 -0700160 parentEntry.first->second.try_emplace(wellKnown, defaultIfaces);
Andrew Geissler70461892019-02-27 09:57:37 -0600161
162 if (!ifaceEntry.second)
163 {
164 // Entry was already there for this name so done.
165 break;
166 }
167
168 pos = parent.find_last_of('/');
169 }
Matt Spinler11401e22019-04-08 13:13:25 -0500170
171 // The new interface might have an association pending
172 checkIfPendingAssociation(objPath.str, interfaceMap, assocMaps, server);
Andrew Geissler70461892019-02-27 09:57:37 -0600173}