1 #include "processing.hpp"
2 
3 #include <boost/algorithm/string/predicate.hpp>
4 #include <iostream>
5 
6 bool 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 }
25 
26 bool 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 }
41 
42 void processNameChangeDelete(
43     boost::container::flat_map<std::string, std::string>& nameOwners,
44     const std::string& wellKnown, const std::string& oldOwner,
45     interface_map_type& interfaceMap, AssociationMaps& assocMaps,
46     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         {
66             auto assoc = std::find_if(
67                 ifaces->second.begin(), ifaces->second.end(),
68                 [](const auto& iface) { return isAssocDefIface(iface); });
69             if (assoc != ifaces->second.end())
70             {
71                 removeAssociation(pathIt->first, wellKnown, server, assocMaps);
72             }
73 
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) &&
79                 (pathIt->second.find("xyz.openbmc_project.ObjectMapper") !=
80                  pathIt->second.end()))
81             {
82                 // Remove the 2 association D-Bus paths and move the
83                 // association to pending.
84                 moveAssociationToPending(pathIt->first, assocMaps, server);
85             }
86         }
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 }
98 
99 void processInterfaceAdded(interface_map_type& interfaceMap,
100                            const sdbusplus::message::object_path& objPath,
101                            const InterfacesAdded& intfAdded,
102                            const std::string& wellKnown,
103                            AssociationMaps& assocMaps,
104                            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 
112         if (isAssocDefIface(interfacePair.first))
113         {
114             const sdbusplus::message::variant<std::vector<Association>>*
115                 variantAssociations = nullptr;
116             for (const auto& interface : interfacePair.second)
117             {
118                 if (interface.first == getAssocDefPropName(interfacePair.first))
119                 {
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 =
130                 sdbusplus::message::variant_ns::get<std::vector<Association>>(
131                     *variantAssociations);
132             associationChanged(server, associations, objPath.str, wellKnown,
133                                interfaceMap, assocMaps);
134         }
135     }
136 
137     // To handle the case where an object path is being created
138     // with 2 or more new path segments, check if the parent paths
139     // of this path are already in the interface map, and add them
140     // if they aren't with just the default freedesktop interfaces.
141     // This would be done via introspection if they would have
142     // already existed at startup.  While we could also introspect
143     // them now to do the work, we know there aren't any other
144     // interfaces or we would have gotten signals for them as well,
145     // so take a shortcut to speed things up.
146     //
147     // This is all needed so that mapper operations can be done
148     // on the new parent paths.
149     using iface_map_iterator = interface_map_type::iterator;
150     using iface_map_value_type =
151         boost::container::flat_map<std::string,
152                                    boost::container::flat_set<std::string>>;
153     using name_map_iterator = iface_map_value_type::iterator;
154 
155     static const boost::container::flat_set<std::string> defaultIfaces{
156         "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
157         "org.freedesktop.DBus.Properties"};
158 
159     std::string parent = objPath.str;
160     auto pos = parent.find_last_of('/');
161 
162     while (pos != std::string::npos)
163     {
164         parent = parent.substr(0, pos);
165 
166         std::pair<iface_map_iterator, bool> parentEntry =
167             interfaceMap.insert(std::make_pair(parent, iface_map_value_type{}));
168 
169         std::pair<name_map_iterator, bool> ifaceEntry =
170             parentEntry.first->second.insert(
171                 std::make_pair(wellKnown, defaultIfaces));
172 
173         if (!ifaceEntry.second)
174         {
175             // Entry was already there for this name so done.
176             break;
177         }
178 
179         pos = parent.find_last_of('/');
180     }
181 
182     // The new interface might have an association pending
183     checkIfPendingAssociation(objPath.str, interfaceMap, assocMaps, server);
184 }
185