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::asio::io_context& io, 44 boost::container::flat_map<std::string, std::string>& nameOwners, 45 const std::string& wellKnown, const std::string& oldOwner, 46 InterfaceMapType& interfaceMap, AssociationMaps& assocMaps, 47 sdbusplus::asio::object_server& server) 48 { 49 if (oldOwner.starts_with(":")) 50 { 51 auto it = nameOwners.find(oldOwner); 52 if (it != nameOwners.end()) 53 { 54 nameOwners.erase(it); 55 } 56 } 57 // Connection removed 58 InterfaceMapType::iterator pathIt = interfaceMap.begin(); 59 while (pathIt != interfaceMap.end()) 60 { 61 // If an associations interface is being removed, 62 // also need to remove the corresponding associations 63 // objects and properties. 64 auto ifaces = pathIt->second.find(wellKnown); 65 if (ifaces != pathIt->second.end()) 66 { 67 auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(), 68 assocDefsInterface); 69 if (assoc != ifaces->second.end()) 70 { 71 removeAssociation(io, pathIt->first, wellKnown, server, 72 assocMaps); 73 } 74 75 // Instead of checking if every single path is the endpoint of an 76 // association that needs to be moved to pending, only check when 77 // we own this path as well, which would be because of an 78 // association. 79 if ((pathIt->second.size() == 2) && 80 (pathIt->second.find("xyz.openbmc_project.ObjectMapper") != 81 pathIt->second.end())) 82 { 83 // Remove the 2 association D-Bus paths and move the 84 // association to pending. 85 moveAssociationToPending(io, pathIt->first, assocMaps, server); 86 } 87 } 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 } 99 100 void processInterfaceAdded(boost::asio::io_context& io, 101 InterfaceMapType& interfaceMap, 102 const sdbusplus::message::object_path& objPath, 103 const InterfacesAdded& intfAdded, 104 const std::string& wellKnown, 105 AssociationMaps& assocMaps, 106 sdbusplus::asio::object_server& server) 107 { 108 auto& ifaceList = interfaceMap[objPath.str]; 109 110 for (const auto& interfacePair : intfAdded) 111 { 112 ifaceList[wellKnown].emplace(interfacePair.first); 113 114 if (interfacePair.first == assocDefsInterface) 115 { 116 const std::variant<std::vector<Association>>* variantAssociations = 117 nullptr; 118 for (const auto& interface : interfacePair.second) 119 { 120 if (interface.first == assocDefsProperty) 121 { 122 variantAssociations = &(interface.second); 123 } 124 } 125 if (variantAssociations == nullptr) 126 { 127 std::cerr << "Illegal association found on " << wellKnown 128 << "\n"; 129 continue; 130 } 131 std::vector<Association> associations = 132 std::get<std::vector<Association>>(*variantAssociations); 133 associationChanged(io, server, associations, objPath.str, wellKnown, 134 interfaceMap, assocMaps); 135 } 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 = InterfaceMapType::iterator; 151 using name_map_iterator = InterfaceMapType::mapped_type::iterator; 152 153 static const InterfaceNames defaultIfaces{ 154 "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer", 155 "org.freedesktop.DBus.Properties"}; 156 157 std::string parent = objPath.str; 158 auto pos = parent.find_last_of('/'); 159 160 while (pos != std::string::npos) 161 { 162 parent = parent.substr(0, pos); 163 164 std::pair<iface_map_iterator, bool> parentEntry = 165 interfaceMap.try_emplace(parent); 166 167 std::pair<name_map_iterator, bool> ifaceEntry = 168 parentEntry.first->second.try_emplace(wellKnown, defaultIfaces); 169 170 if (!ifaceEntry.second) 171 { 172 // Entry was already there for this name so done. 173 break; 174 } 175 176 pos = parent.find_last_of('/'); 177 } 178 179 // The new interface might have an association pending 180 checkIfPendingAssociation(io, objPath.str, interfaceMap, assocMaps, server); 181 } 182