blob: 2dfebfde9eed7d92bbc3e6e278daf866fedc8e4d [file] [log] [blame]
Matt Spinler5eddf442019-04-10 10:08:56 -05001#include "config.h"
2
Andrew Geissler3b025e62019-02-01 10:33:54 -06003#include "processing.hpp"
4
5#include <boost/algorithm/string/predicate.hpp>
Andrew Geissler70461892019-02-27 09:57:37 -06006#include <iostream>
Andrew Geissler3b025e62019-02-01 10:33:54 -06007
8bool getWellKnown(
9 const boost::container::flat_map<std::string, std::string>& owners,
10 const std::string& request, std::string& wellKnown)
11{
12 // If it's already a well known name, just return
13 if (!boost::starts_with(request, ":"))
14 {
15 wellKnown = request;
16 return true;
17 }
18
19 auto it = owners.find(request);
20 if (it == owners.end())
21 {
22 return false;
23 }
24 wellKnown = it->second;
25 return true;
26}
Andrew Geissler82815da2019-02-04 12:19:41 -060027
28bool needToIntrospect(const std::string& processName,
29 const WhiteBlackList& whiteList,
30 const WhiteBlackList& blackList)
31{
32 auto inWhitelist =
33 std::find_if(whiteList.begin(), whiteList.end(),
34 [&processName](const auto& prefix) {
35 return boost::starts_with(processName, prefix);
36 }) != whiteList.end();
37
38 // This holds full service names, not prefixes
39 auto inBlacklist = blackList.find(processName) != blackList.end();
40
41 return inWhitelist && !inBlacklist;
42}
Andrew Geissler20679262019-02-11 20:20:40 -060043
44void processNameChangeDelete(
45 boost::container::flat_map<std::string, std::string>& nameOwners,
46 const std::string& wellKnown, const std::string& oldOwner,
Matt Spinlere2359fb2019-04-05 14:11:33 -050047 interface_map_type& interfaceMap, AssociationMaps& assocMaps,
Andrew Geissler20679262019-02-11 20:20:40 -060048 sdbusplus::asio::object_server& server)
49{
50 if (boost::starts_with(oldOwner, ":"))
51 {
52 auto it = nameOwners.find(oldOwner);
53 if (it != nameOwners.end())
54 {
55 nameOwners.erase(it);
56 }
57 }
58 // Connection removed
59 interface_map_type::iterator pathIt = interfaceMap.begin();
60 while (pathIt != interfaceMap.end())
61 {
62 // If an associations interface is being removed,
63 // also need to remove the corresponding associations
64 // objects and properties.
65 auto ifaces = pathIt->second.find(wellKnown);
66 if (ifaces != pathIt->second.end())
67 {
Matt Spinler8f876a52019-04-15 13:22:50 -050068 auto assoc = std::find_if(
69 ifaces->second.begin(), ifaces->second.end(),
70 [](const auto& iface) { return isAssocDefIface(iface); });
Andrew Geissler20679262019-02-11 20:20:40 -060071 if (assoc != ifaces->second.end())
72 {
Matt Spinlere2359fb2019-04-05 14:11:33 -050073 removeAssociation(pathIt->first, wellKnown, server, assocMaps);
Andrew Geissler20679262019-02-11 20:20:40 -060074 }
Matt Spinler9c3d2852019-04-08 15:57:19 -050075
76 // Instead of checking if every single path is the endpoint of an
77 // association that needs to be moved to pending, only check when
78 // we own this path as well, which would be because of an
79 // association.
80 if ((pathIt->second.size() == 2) &&
Matt Spinler5eddf442019-04-10 10:08:56 -050081 (pathIt->second.find(MAPPER_BUSNAME) != pathIt->second.end()))
Matt Spinler9c3d2852019-04-08 15:57:19 -050082 {
83 // Remove the 2 association D-Bus paths and move the
84 // association to pending.
85 moveAssociationToPending(pathIt->first, assocMaps, server);
86 }
Andrew Geissler20679262019-02-11 20:20:40 -060087 }
88 pathIt->second.erase(wellKnown);
89 if (pathIt->second.empty())
90 {
91 // If the last connection to the object is gone,
92 // delete the top level object
93 pathIt = interfaceMap.erase(pathIt);
94 continue;
95 }
96 pathIt++;
97 }
98}
Andrew Geissler70461892019-02-27 09:57:37 -060099
100void processInterfaceAdded(interface_map_type& interfaceMap,
101 const sdbusplus::message::object_path& objPath,
102 const InterfacesAdded& intfAdded,
103 const std::string& wellKnown,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500104 AssociationMaps& assocMaps,
Andrew Geissler70461892019-02-27 09:57:37 -0600105 sdbusplus::asio::object_server& server)
106{
107 auto& ifaceList = interfaceMap[objPath.str];
108
109 for (const auto& interfacePair : intfAdded)
110 {
111 ifaceList[wellKnown].emplace(interfacePair.first);
112
Matt Spinler8f876a52019-04-15 13:22:50 -0500113 if (isAssocDefIface(interfacePair.first))
Andrew Geissler70461892019-02-27 09:57:37 -0600114 {
115 const sdbusplus::message::variant<std::vector<Association>>*
116 variantAssociations = nullptr;
117 for (const auto& interface : interfacePair.second)
118 {
Matt Spinler8f876a52019-04-15 13:22:50 -0500119 if (interface.first == getAssocDefPropName(interfacePair.first))
Andrew Geissler70461892019-02-27 09:57:37 -0600120 {
121 variantAssociations = &(interface.second);
122 }
123 }
124 if (variantAssociations == nullptr)
125 {
126 std::cerr << "Illegal association found on " << wellKnown
127 << "\n";
128 continue;
129 }
130 std::vector<Association> associations =
131 sdbusplus::message::variant_ns::get<std::vector<Association>>(
132 *variantAssociations);
133 associationChanged(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.
150 using iface_map_iterator = interface_map_type::iterator;
151 using iface_map_value_type =
152 boost::container::flat_map<std::string,
153 boost::container::flat_set<std::string>>;
154 using name_map_iterator = iface_map_value_type::iterator;
155
156 static const boost::container::flat_set<std::string> defaultIfaces{
157 "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
158 "org.freedesktop.DBus.Properties"};
159
160 std::string parent = objPath.str;
161 auto pos = parent.find_last_of('/');
162
163 while (pos != std::string::npos)
164 {
165 parent = parent.substr(0, pos);
166
167 std::pair<iface_map_iterator, bool> parentEntry =
168 interfaceMap.insert(std::make_pair(parent, iface_map_value_type{}));
169
170 std::pair<name_map_iterator, bool> ifaceEntry =
171 parentEntry.first->second.insert(
172 std::make_pair(wellKnown, defaultIfaces));
173
174 if (!ifaceEntry.second)
175 {
176 // Entry was already there for this name so done.
177 break;
178 }
179
180 pos = parent.find_last_of('/');
181 }
Matt Spinler11401e22019-04-08 13:13:25 -0500182
183 // The new interface might have an association pending
184 checkIfPendingAssociation(objPath.str, interfaceMap, assocMaps, server);
Andrew Geissler70461892019-02-27 09:57:37 -0600185}