Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 1 | #include "processing.hpp" |
| 2 | |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 3 | #include <algorithm> |
| 4 | #include <array> |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 5 | #include <iostream> |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 6 | #include <string> |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 7 | #include <string_view> |
Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 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 |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 14 | if (!request.starts_with(":")) |
Andrew Geissler | 3b025e6 | 2019-02-01 10:33:54 -0600 | [diff] [blame] | 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 | } |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 28 | |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 29 | bool needToIntrospect(const std::string& processName) |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 30 | { |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 31 | using namespace std::string_view_literals; |
| 32 | static constexpr std::array<std::string_view, 2> skipNamespaces{ |
| 33 | ":"sv, "org.freedesktop"sv}; |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 34 | |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 35 | auto inSkipList = std::find_if(skipNamespaces.begin(), skipNamespaces.end(), |
| 36 | [&processName](auto prefix) { |
Patrick Williams | 9052ebd | 2024-08-16 15:22:16 -0400 | [diff] [blame] | 37 | return processName.starts_with(prefix); |
| 38 | }) != skipNamespaces.end(); |
Brad Bishop | 1e94e60 | 2022-06-02 19:47:53 -0400 | [diff] [blame] | 39 | return !(inSkipList || processName.empty()); |
Andrew Geissler | 82815da | 2019-02-04 12:19:41 -0600 | [diff] [blame] | 40 | } |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 41 | |
| 42 | void processNameChangeDelete( |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 43 | boost::asio::io_context& io, |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 44 | boost::container::flat_map<std::string, std::string>& nameOwners, |
| 45 | const std::string& wellKnown, const std::string& oldOwner, |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 46 | InterfaceMapType& interfaceMap, AssociationMaps& assocMaps, |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 47 | sdbusplus::asio::object_server& server) |
| 48 | { |
Brad Bishop | 86d2880 | 2022-07-11 15:49:31 -0400 | [diff] [blame] | 49 | if (oldOwner.starts_with(":")) |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 50 | { |
| 51 | auto it = nameOwners.find(oldOwner); |
| 52 | if (it != nameOwners.end()) |
| 53 | { |
| 54 | nameOwners.erase(it); |
| 55 | } |
| 56 | } |
| 57 | // Connection removed |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 58 | InterfaceMapType::iterator pathIt = interfaceMap.begin(); |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 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 | { |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 67 | auto assoc = std::find(ifaces->second.begin(), ifaces->second.end(), |
| 68 | assocDefsInterface); |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 69 | if (assoc != ifaces->second.end()) |
| 70 | { |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 71 | removeAssociation(io, pathIt->first, wellKnown, server, |
| 72 | assocMaps); |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 73 | } |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 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) && |
Brad Bishop | a02cd54 | 2021-10-12 19:12:42 -0400 | [diff] [blame] | 80 | (pathIt->second.find("xyz.openbmc_project.ObjectMapper") != |
| 81 | pathIt->second.end())) |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 82 | { |
| 83 | // Remove the 2 association D-Bus paths and move the |
| 84 | // association to pending. |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 85 | moveAssociationToPending(io, pathIt->first, assocMaps, server); |
Matt Spinler | 9c3d285 | 2019-04-08 15:57:19 -0500 | [diff] [blame] | 86 | } |
Andrew Geissler | 2067926 | 2019-02-11 20:20:40 -0600 | [diff] [blame] | 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 | } |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 99 | |
Patrick Williams | 9052ebd | 2024-08-16 15:22:16 -0400 | [diff] [blame] | 100 | void processInterfaceAdded( |
| 101 | boost::asio::io_context& io, InterfaceMapType& interfaceMap, |
| 102 | const sdbusplus::message::object_path& objPath, |
| 103 | const InterfacesAdded& intfAdded, const std::string& wellKnown, |
| 104 | AssociationMaps& assocMaps, sdbusplus::asio::object_server& server) |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 105 | { |
| 106 | auto& ifaceList = interfaceMap[objPath.str]; |
| 107 | |
| 108 | for (const auto& interfacePair : intfAdded) |
| 109 | { |
| 110 | ifaceList[wellKnown].emplace(interfacePair.first); |
| 111 | |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 112 | if (interfacePair.first == assocDefsInterface) |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 113 | { |
Patrick Williams | 2bb2d6b | 2020-05-13 17:59:02 -0500 | [diff] [blame] | 114 | const std::variant<std::vector<Association>>* variantAssociations = |
| 115 | nullptr; |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 116 | for (const auto& interface : interfacePair.second) |
| 117 | { |
John Wang | d0cf942 | 2019-09-17 16:01:34 +0800 | [diff] [blame] | 118 | if (interface.first == assocDefsProperty) |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 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 = |
Patrick Williams | b05bc12 | 2020-05-13 12:21:00 -0500 | [diff] [blame] | 130 | std::get<std::vector<Association>>(*variantAssociations); |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 131 | associationChanged(io, server, associations, objPath.str, wellKnown, |
Matt Spinler | e0b0e3a | 2019-04-08 10:39:23 -0500 | [diff] [blame] | 132 | interfaceMap, assocMaps); |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | // To handle the case where an object path is being created |
| 137 | // with 2 or more new path segments, check if the parent paths |
| 138 | // of this path are already in the interface map, and add them |
| 139 | // if they aren't with just the default freedesktop interfaces. |
| 140 | // This would be done via introspection if they would have |
| 141 | // already existed at startup. While we could also introspect |
| 142 | // them now to do the work, we know there aren't any other |
| 143 | // interfaces or we would have gotten signals for them as well, |
| 144 | // so take a shortcut to speed things up. |
| 145 | // |
| 146 | // This is all needed so that mapper operations can be done |
| 147 | // on the new parent paths. |
Brad Bishop | a098a37 | 2022-05-05 15:19:04 -0400 | [diff] [blame] | 148 | using iface_map_iterator = InterfaceMapType::iterator; |
Ed Tanous | ea0e5d2 | 2024-11-11 10:00:56 -0800 | [diff] [blame] | 149 | using name_map_iterator = ConnectionNames::iterator; |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 150 | |
Ed Tanous | 964681c | 2022-07-08 12:47:24 -0700 | [diff] [blame] | 151 | static const InterfaceNames defaultIfaces{ |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 152 | "org.freedesktop.DBus.Introspectable", "org.freedesktop.DBus.Peer", |
| 153 | "org.freedesktop.DBus.Properties"}; |
| 154 | |
| 155 | std::string parent = objPath.str; |
| 156 | auto pos = parent.find_last_of('/'); |
| 157 | |
| 158 | while (pos != std::string::npos) |
| 159 | { |
| 160 | parent = parent.substr(0, pos); |
| 161 | |
| 162 | std::pair<iface_map_iterator, bool> parentEntry = |
Ed Tanous | ea0e5d2 | 2024-11-11 10:00:56 -0800 | [diff] [blame] | 163 | interfaceMap.emplace(parent, ConnectionNames{}); |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 164 | |
| 165 | std::pair<name_map_iterator, bool> ifaceEntry = |
Ed Tanous | ea0e5d2 | 2024-11-11 10:00:56 -0800 | [diff] [blame] | 166 | parentEntry.first->second.emplace(wellKnown, defaultIfaces); |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 167 | |
| 168 | if (!ifaceEntry.second) |
| 169 | { |
| 170 | // Entry was already there for this name so done. |
| 171 | break; |
| 172 | } |
| 173 | |
| 174 | pos = parent.find_last_of('/'); |
| 175 | } |
Matt Spinler | 11401e2 | 2019-04-08 13:13:25 -0500 | [diff] [blame] | 176 | |
| 177 | // The new interface might have an association pending |
Kallas, Pawel | 5b4357d | 2022-10-12 15:36:37 +0200 | [diff] [blame] | 178 | checkIfPendingAssociation(io, objPath.str, interfaceMap, assocMaps, server); |
Andrew Geissler | 7046189 | 2019-02-27 09:57:37 -0600 | [diff] [blame] | 179 | } |