1 #include "processing.hpp"
2 
3 #include <algorithm>
4 #include <array>
5 #include <iostream>
6 #include <string>
7 #include <string_view>
8 
9 bool getWellKnown(
10     const boost::container::flat_map<std::string, std::string>& owners,
11     const std::string& request, std::string& wellKnown)
12 {
13     // If it's already a well known name, just return
14     if (!request.starts_with(":"))
15     {
16         wellKnown = request;
17         return true;
18     }
19 
20     auto it = owners.find(request);
21     if (it == owners.end())
22     {
23         return false;
24     }
25     wellKnown = it->second;
26     return true;
27 }
28 
29 bool needToIntrospect(const std::string& processName)
30 {
31     using namespace std::string_view_literals;
32     static constexpr std::array<std::string_view, 2> skipNamespaces{
33         ":"sv, "org.freedesktop"sv};
34 
35     auto inSkipList = std::find_if(skipNamespaces.begin(), skipNamespaces.end(),
36                                    [&processName](auto prefix) {
37                                        return processName.starts_with(prefix);
38                                    }) != skipNamespaces.end();
39     return !(inSkipList || processName.empty());
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     InterfaceMapType& interfaceMap, AssociationMaps& assocMaps,
46     sdbusplus::asio::object_server& server)
47 {
48     if (oldOwner.starts_with(":"))
49     {
50         auto it = nameOwners.find(oldOwner);
51         if (it != nameOwners.end())
52         {
53             nameOwners.erase(it);
54         }
55     }
56     // Connection removed
57     InterfaceMapType::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(ifaces->second.begin(), ifaces->second.end(),
67                                    assocDefsInterface);
68             if (assoc != ifaces->second.end())
69             {
70                 removeAssociation(pathIt->first, wellKnown, server, assocMaps);
71             }
72 
73             // Instead of checking if every single path is the endpoint of an
74             // association that needs to be moved to pending, only check when
75             // we own this path as well, which would be because of an
76             // association.
77             if ((pathIt->second.size() == 2) &&
78                 (pathIt->second.find("xyz.openbmc_project.ObjectMapper") !=
79                  pathIt->second.end()))
80             {
81                 // Remove the 2 association D-Bus paths and move the
82                 // association to pending.
83                 moveAssociationToPending(pathIt->first, assocMaps, server);
84             }
85         }
86         pathIt->second.erase(wellKnown);
87         if (pathIt->second.empty())
88         {
89             // If the last connection to the object is gone,
90             // delete the top level object
91             pathIt = interfaceMap.erase(pathIt);
92             continue;
93         }
94         pathIt++;
95     }
96 }
97 
98 void processInterfaceAdded(InterfaceMapType& interfaceMap,
99                            const sdbusplus::message::object_path& objPath,
100                            const InterfacesAdded& intfAdded,
101                            const std::string& wellKnown,
102                            AssociationMaps& assocMaps,
103                            sdbusplus::asio::object_server& server)
104 {
105     auto& ifaceList = interfaceMap[objPath.str];
106 
107     for (const auto& interfacePair : intfAdded)
108     {
109         ifaceList[wellKnown].emplace(interfacePair.first);
110 
111         if (interfacePair.first == assocDefsInterface)
112         {
113             const std::variant<std::vector<Association>>* variantAssociations =
114                 nullptr;
115             for (const auto& interface : interfacePair.second)
116             {
117                 if (interface.first == assocDefsProperty)
118                 {
119                     variantAssociations = &(interface.second);
120                 }
121             }
122             if (variantAssociations == nullptr)
123             {
124                 std::cerr << "Illegal association found on " << wellKnown
125                           << "\n";
126                 continue;
127             }
128             std::vector<Association> associations =
129                 std::get<std::vector<Association>>(*variantAssociations);
130             associationChanged(server, associations, objPath.str, wellKnown,
131                                interfaceMap, assocMaps);
132         }
133     }
134 
135     // To handle the case where an object path is being created
136     // with 2 or more new path segments, check if the parent paths
137     // of this path are already in the interface map, and add them
138     // if they aren't with just the default freedesktop interfaces.
139     // This would be done via introspection if they would have
140     // already existed at startup.  While we could also introspect
141     // them now to do the work, we know there aren't any other
142     // interfaces or we would have gotten signals for them as well,
143     // so take a shortcut to speed things up.
144     //
145     // This is all needed so that mapper operations can be done
146     // on the new parent paths.
147     using iface_map_iterator = InterfaceMapType::iterator;
148     using name_map_iterator = InterfaceMapType::mapped_type::iterator;
149 
150     static const InterfaceNames defaultIfaces{
151         "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer",
152         "org.freedesktop.DBus.Properties"};
153 
154     std::string parent = objPath.str;
155     auto pos = parent.find_last_of('/');
156 
157     while (pos != std::string::npos)
158     {
159         parent = parent.substr(0, pos);
160 
161         std::pair<iface_map_iterator, bool> parentEntry =
162             interfaceMap.try_emplace(parent);
163 
164         std::pair<name_map_iterator, bool> ifaceEntry =
165             parentEntry.first->second.try_emplace(wellKnown, defaultIfaces);
166 
167         if (!ifaceEntry.second)
168         {
169             // Entry was already there for this name so done.
170             break;
171         }
172 
173         pos = parent.find_last_of('/');
174     }
175 
176     // The new interface might have an association pending
177     checkIfPendingAssociation(objPath.str, interfaceMap, assocMaps, server);
178 }
179