blob: 4d2d3c14786985f1a59dac92989c18fe97720d0b [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 {
Matt Spinler8f876a52019-04-15 13:22:50 -050066 auto assoc = std::find_if(
67 ifaces->second.begin(), ifaces->second.end(),
68 [](const auto& iface) { return isAssocDefIface(iface); });
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 }
73 }
74 pathIt->second.erase(wellKnown);
75 if (pathIt->second.empty())
76 {
77 // If the last connection to the object is gone,
78 // delete the top level object
79 pathIt = interfaceMap.erase(pathIt);
80 continue;
81 }
82 pathIt++;
83 }
84}
Andrew Geissler70461892019-02-27 09:57:37 -060085
86void processInterfaceAdded(interface_map_type& interfaceMap,
87 const sdbusplus::message::object_path& objPath,
88 const InterfacesAdded& intfAdded,
89 const std::string& wellKnown,
Matt Spinlere2359fb2019-04-05 14:11:33 -050090 AssociationMaps& assocMaps,
Andrew Geissler70461892019-02-27 09:57:37 -060091 sdbusplus::asio::object_server& server)
92{
93 auto& ifaceList = interfaceMap[objPath.str];
94
95 for (const auto& interfacePair : intfAdded)
96 {
97 ifaceList[wellKnown].emplace(interfacePair.first);
98
Matt Spinler8f876a52019-04-15 13:22:50 -050099 if (isAssocDefIface(interfacePair.first))
Andrew Geissler70461892019-02-27 09:57:37 -0600100 {
101 const sdbusplus::message::variant<std::vector<Association>>*
102 variantAssociations = nullptr;
103 for (const auto& interface : interfacePair.second)
104 {
Matt Spinler8f876a52019-04-15 13:22:50 -0500105 if (interface.first == getAssocDefPropName(interfacePair.first))
Andrew Geissler70461892019-02-27 09:57:37 -0600106 {
107 variantAssociations = &(interface.second);
108 }
109 }
110 if (variantAssociations == nullptr)
111 {
112 std::cerr << "Illegal association found on " << wellKnown
113 << "\n";
114 continue;
115 }
116 std::vector<Association> associations =
117 sdbusplus::message::variant_ns::get<std::vector<Association>>(
118 *variantAssociations);
119 associationChanged(server, associations, objPath.str, wellKnown,
Matt Spinlere2359fb2019-04-05 14:11:33 -0500120 assocMaps);
Andrew Geissler70461892019-02-27 09:57:37 -0600121 }
122 }
123
124 // To handle the case where an object path is being created
125 // with 2 or more new path segments, check if the parent paths
126 // of this path are already in the interface map, and add them
127 // if they aren't with just the default freedesktop interfaces.
128 // This would be done via introspection if they would have
129 // already existed at startup. While we could also introspect
130 // them now to do the work, we know there aren't any other
131 // interfaces or we would have gotten signals for them as well,
132 // so take a shortcut to speed things up.
133 //
134 // This is all needed so that mapper operations can be done
135 // on the new parent paths.
136 using iface_map_iterator = interface_map_type::iterator;
137 using iface_map_value_type =
138 boost::container::flat_map<std::string,
139 boost::container::flat_set<std::string>>;
140 using name_map_iterator = iface_map_value_type::iterator;
141
142 static const boost::container::flat_set<std::string> defaultIfaces{
143 "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
144 "org.freedesktop.DBus.Properties"};
145
146 std::string parent = objPath.str;
147 auto pos = parent.find_last_of('/');
148
149 while (pos != std::string::npos)
150 {
151 parent = parent.substr(0, pos);
152
153 std::pair<iface_map_iterator, bool> parentEntry =
154 interfaceMap.insert(std::make_pair(parent, iface_map_value_type{}));
155
156 std::pair<name_map_iterator, bool> ifaceEntry =
157 parentEntry.first->second.insert(
158 std::make_pair(wellKnown, defaultIfaces));
159
160 if (!ifaceEntry.second)
161 {
162 // Entry was already there for this name so done.
163 break;
164 }
165
166 pos = parent.find_last_of('/');
167 }
168}